除了已经给出的好的答案之外,这里还有一个如何 获得简单数组结构的示例。 (您可以使用例如Goz' code 进行迭代。)
GetDIBits reference @ MSDN
您必须选择DIB_RGB_COLORS 作为uUsage 的标志并设置BITMAPINFO structure 和它包含的BITMAPINFOHEADER structure。当您将biClrUsed 和biClrImportant 设置为零时,没有“无”颜色表,因此您可以读取从GetDIBits 获得的位图像素作为RGB 值序列。使用32作为位数(biBitCount)根据MSDN设置数据结构:
位图最多有 2^32 种颜色。如果BITMAPINFOHEADER 的biCompression 成员是BI_RGB,则BITMAPINFO 的bmiColors 成员是NULL。位图数组中的每个DWORD 分别代表一个像素的蓝色、绿色和红色的相对强度。每个DWORD中的高字节没有被使用。
由于 MS LONG 正好是 32 位长(DWORD 的大小),因此您不必注意填充(如 Remarks section 中所述)。
代码:
HDC hdcSource = NULL; // the source device context
HBITMAP hSource = NULL; // the bitmap selected into the device context
BITMAPINFO MyBMInfo = {0};
MyBMInfo.bmiHeader.biSize = sizeof(MyBMInfo.bmiHeader);
// Get the BITMAPINFO structure from the bitmap
if(0 == GetDIBits(hdcSource, hSource, 0, 0, NULL, &MyBMInfo, DIB_RGB_COLORS))
{
// error handling
}
// create the pixel buffer
BYTE* lpPixels = new BYTE[MyBMInfo.bmiHeader.biSizeImage];
// We'll change the received BITMAPINFOHEADER to request the data in a
// 32 bit RGB format (and not upside-down) so that we can iterate over
// the pixels easily.
// requesting a 32 bit image means that no stride/padding will be necessary,
// although it always contains an (possibly unused) alpha channel
MyBMInfo.bmiHeader.biBitCount = 32;
MyBMInfo.bmiHeader.biCompression = BI_RGB; // no compression -> easier to use
// correct the bottom-up ordering of lines (abs is in cstdblib and stdlib.h)
MyBMInfo.bmiHeader.biHeight = abs(MyBMInfo.bmiHeader.biHeight);
// Call GetDIBits a second time, this time to (format and) store the actual
// bitmap data (the "pixels") in the buffer lpPixels
if(0 == GetDIBits(hdcSource, hSource, 0, MyBMInfo.bmiHeader.biHeight,
lpPixels, &MyBMInfo, DIB_RGB_COLORS))
{
// error handling
}
// clean up: deselect bitmap from device context, close handles, delete buffer