Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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 8:45am
aryx123702

1 solution

Rate this: bad
good
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).
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 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 at 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
0 OriginalGriff 277
1 BillWoodruff 205
2 Maciej Los 205
3 DamithSL 165
4 Sergey Alexandrovich Kryukov 145
0 OriginalGriff 5,107
1 DamithSL 4,157
2 Maciej Los 3,595
3 Kornfeld Eliyahu Peter 3,450
4 Sergey Alexandrovich Kryukov 2,741


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 23 Sep 2012
Copyright © CodeProject, 1999-2014
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