Click here to Skip to main content
15,886,801 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

C#
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

1 solution

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).
 
Share this answer
 
Comments
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...
--SA
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900