c ++ GetDIBits不起作用 [英] c++ GetDIBits not working
问题描述
首先我加载图像cool.bmp"..加载很好.然后我调用函数getPixArray"但它失败了.
First i load the image "cool.bmp".. load is fine. then i call the function "getPixArray" but it fails.
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 函数
my getPixArray function
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;
}
该函数应该获取对用于绘制图像的内部像素数组的引用.但是两个失败"消息都会打印到控制台.任何人都可以识别错误或更好地生成此功能的工作版本以便我可以从中学习吗?我被困在这个问题上好几天了,请帮忙!
This function is supposed to get a reference to the internal pixel array used to draw the image. but both 'FAIL' messages print to the console. Can anyone identify the error or better produce a working version of this function so i can learn from it? ive been stuck for days on this, please help!
这是我从以下位置获得的大部分代码:GetDIBits 和循环通过像素使用 X, Y
This is the were i got most of this code from: GetDIBits and loop through pixels using X, Y
这是我使用的图像:cool.bmp"是一个 24 位位图.宽度:204 高度:204
This is the image i used: "cool.bmp" is a 24-bit Bitmap. Width:204 Height: 204
推荐答案
您的第一个函数调用失败,因为您没有初始化 MyBMInfo.bmiHeader.biSize
.你需要这样做:
Your first function call fails because you did not initialise MyBMInfo.bmiHeader.biSize
. You need to do this:
...
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))
....
一旦你解决了这个问题,其余的代码就会按预期工作.
Once you fix that, the rest of the code will work as intended.
这篇关于c ++ GetDIBits不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!