首先我加载图像"cool.bmp"..装货没问题。然后我调用函数"getPixArray“,但它失败了。
case WM_CREATE:// runs once on creation of window
hBitmap = (HBITMAP)LoadImage(NULL, L"cool.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE );
if(hBitmap == NULL)
::printToDebugWindow("Error: loading bitmap\n");
else
BYTE* b = ::getPixArray(hBitmap); 我的getPixArray函数
BYTE* getPixArray(HBITMAP hBitmap)
{
HDC hdc,hdcMem;
hdc = GetDC(NULL);
hdcMem = CreateCompatibleDC(hdc);
BITMAPINFO MyBMInfo = {0};
// Get the BITMAPINFO structure from the bitmap
if(0 == GetDIBits(hdcMem, hBitmap, 0, 0, NULL, &MyBMInfo, DIB_RGB_COLORS))
{
::printToDebugWindow("FAIL\n");
}
// create the bitmap buffer
BYTE* lpPixels = new BYTE[MyBMInfo.bmiHeader.biSizeImage];
MyBMInfo.bmiHeader.biSize = sizeof(MyBMInfo.bmiHeader);
MyBMInfo.bmiHeader.biBitCount = 32;
MyBMInfo.bmiHeader.biCompression = BI_RGB;
MyBMInfo.bmiHeader.biHeight = (MyBMInfo.bmiHeader.biHeight < 0) ? (-MyBMInfo.bmiHeader.biHeight) : (MyBMInfo.bmiHeader.biHeight);
// get the actual bitmap buffer
if(0 == GetDIBits(hdc, hBitmap, 0, MyBMInfo.bmiHeader.biHeight, (LPVOID)lpPixels, &MyBMInfo, DIB_RGB_COLORS))
{
::printToDebugWindow("FAIL\n");
}
return lpPixels;
}此函数用于获取用于绘制图像的内部像素数组的引用。但这两条“失败”消息都会打印到控制台。有没有人可以找出错误,或者更好地生成这个函数的工作版本,以便我可以从中学习?我已经被困在这个问题上好几天了,请帮帮我!
这是我从GetDIBits and loop through pixels using X, Y获得大部分代码的地方
这是我使用的图像:"cool.bmp“是一个24位的位图。宽: 204高:204

发布于 2013-06-16 05:09:17
您的第一个函数调用失败,因为您没有初始化MyBMInfo.bmiHeader.biSize。您需要这样做:
...
BITMAPINFO MyBMInfo = {0};
MyBMInfo.bmiHeader.biSize = sizeof(MyBMInfo.bmiHeader);
// Get the BITMAPINFO structure from the bitmap
if(0 == GetDIBits(hdcMem, hBitmap, 0, 0, NULL, &MyBMInfo, DIB_RGB_COLORS))
....一旦你解决了这个问题,剩下的代码就可以正常工作了。
https://stackoverflow.com/questions/16991370
复制相似问题