Click here to Skip to main content
12,887,768 members (39,200 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# C#4.0
Hellow, i`m trying to find the last pixel that is not black and starting from there i create a rectangle and for that i use this function, i used the unsafe method to get pixels and i get this error :
"{"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."}". I`m not an experienced programer, maby a more experienced eye will see the problem can someone tell me what is the cause of that error and a way to solve it?

public unsafe static Rectangle getRectangle(Bitmap b)
            Rectangle rct = new Rectangle();
            rct.Width = 40;
            rct.Height = 40;
            int x = 0;
            int y = 0;
            BitmapData bmd = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadOnly, b.PixelFormat);
            int pixelSize = GetBitsPerPixel(b.PixelFormat);//gets the number of bits per pixel
            for (int j = 0; j < bmd.Height; j++)
                byte* row = (byte*)bmd.Scan0 + (j * bmd.Stride);
                for (int i = 0; i < bmd.Stride; i++)
                    int offSet = i * pixelSize;
                    byte blue = row[offSet];//here i get the error!!!!
                    byte green = row[offSet + 1];
                    byte red = row[offSet + 2];
                    if ((green > 0) && (blue> 0) && (red > 0))
                        x = i;
                        y = j;
            rct.X = x;
            rct.Y = y;
            return rct;
Posted 23-Sep-12 7:45am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

There are many problems.
The stride means how many bytes each line occupies in memory. But to look for all pixels, you should still use the bmp.Width, not the stride in the for.

In your case, if the image is 24 bits you will have a stride 3 times larger than your width.
Also, you read the image as a 24 bit rgb (one byte for red, one for green, one for blue), but at the same time you never asked the image to be in 24-bits format. It can very well be in 16-bit, 8-bit or 32-bit.
The easiest solution is to consider the image is always 24-bits, but then use the 24bit format on the LockBits (and you will not need to check the BitsPerPixel... it will be 24).
Sergey Alexandrovich Kryukov 23-Sep-12 21:57pm
The problem is detected, my 5.
One note on the stride vs bmp.Width. The stride is related to memory, the Width is the number of pixels. So one can still look at the stride and PixelFormat, which should be known for the manipulations anyway...
Paulo Zemek 24-Sep-12 8:52am
Exactly... in fact, the stride is how many bytes a line utilises, and it is not necessarely the Width * bytes per pixel because of memory alignement. Sometimes it will be a multiple of 8 even when the image width is not a multiple of height (for example, the image is 77 pixels, but the Stride is for an image of 80 pixels). So, it is important to use the stride to find the beginning of a line (line index * stride).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170424.1 | Last Updated 23 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100