I\'m grabbing a portion of the screen and scanning through the pixels for a certain color range.
I looked at MSDN\'s Capturing an Image example and know how to use t
GetDIBits
returns a one-dimensional array of values. For a bitmap that's M pixels wide by N pixels tall and uses 24-bit color, the first (M*3) bytes will be the first row of pixels. That can be followed by some padding bytes. It depends on the BITMAPINFOHEADER. There is usually padding to make the width a multiple of 4 bytes. So if your bitmap is 33 pixels wide, there will actually be (36*3) bytes per row.
This "pixels plus padding" is called the "stride". For RGB bitmaps, you can calculate stride with: stride = (biWidth * (biBitCount / 8) + 3) & ~3
, where biWidth
and biBitCount
are taken from the BITMAPINFOHEADER.
I'm not sure how you want to traverse the array. If you want to go pixel-by-pixel from top left to lower right (assuming this is a top-down bitmap):
for (row = 0; row < Image.Height; ++row)
{
int rowBase = row*stride;
for (col = 0; col < Image.Width; ++col)
{
red = lpPixels[rowBase + col];
// etc.
}
}