除了已经给出的好的答案之外,这里还有一个例子how得到一个简单的数组结构来行走。 (您可以使用例如戈兹的代码用于迭代。)
GetDIBits 参考@MSDN
你必须选择DIB_RGB_COLORS
作为标志uUsage
并设置BITMAPINFO结构和BITMAPINFOHEADER结构它包含。当你设置biClrUsed
and biClrImportant
为零,则“无”颜色表,因此您可以读取从中获取的位图的像素GetDIBits
作为 RGB 值的序列。使用32
作为位数(biBitCount
)根据MSDN设置数据结构:
位图最多有 2^32 种颜色。如果biCompression
的成员BITMAPINFOHEADER
is BI_RGB
, the bmiColors
成员BITMAPINFO
is NULL
. Each DWORD
位图数组中的 分别表示像素的蓝色、绿色和红色的相对强度。每个中的高字节DWORD
未使用。
自从成为 MSLONG
正好是 32 位长(a 的大小DWORD
),您不必注意填充(如备注部分).
Code:
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