4

首先我加载图像“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 在此处输入图像描述

4

1 回答 1

8

您的第一个函数调用失败,因为您没有初始化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))
....

一旦你解决了这个问题,其余的代码将按预期工作。

于 2013-06-15T21:09:16.673 回答