I got a problem with Lockbits. I’m searching black pixels, and because it does not seem right, I’m using a WindowPopup to print the colors of every pixel, but it’s like my program is using another picture.
alt text http://i208.photobucket.com/albums/bb91/Savaronna/pixel-1.jpg?t=1234874238
I marked the first found black pixel red. As you can see there are several other pixels that should match too. What am I doing wrong?
This is my script, do I oversee something?
Bitmap b = this.TableListBMP; BitmapData bmpData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppPArgb); int stride = bmpData.Stride; IntPtr Scan0 = bmpData.Scan0; unsafe { byte * p = (byte *)(void *)Scan0; int nOffset = bmpData.Stride - b.Width*4; int xOffset, yOffset; for(int y=5; y<b.Height; ++y) { for(int x=1; x<b.Width; ++x) { MessageBox.Show( string.Format('x={0}, y={1}, ARGB={2},{3},{4},{5}', x, y, Convert.ToString(p[(y*stride)+(x*4)]), Convert.ToString(p[(y*stride)+(x*4)+1]), Convert.ToString(p[(y*stride)+(x*4)+2]), Convert.ToString(p[(y*stride)+(x*4)+3]))); p +=4; } // EDIT: This line should be removed p += nOffset; } } b.UnlockBits(bmpData);
If you are using offsets such as
p[((y)*stride)+((x)*4)], you probably shouldn’t be increasing p at all? Surely you’ve already handled that (xandy) with the multiplication? Also,bmpData.Stride - b.Width*4is not (as far as I can see) a meaningful number… the stride is the rows size including padding.Either remove the ‘
p += ...‘ code, or change the offset algorithm. At the moment you are skipping data (bad) and accessing data outside of the object (very bad).