2

我试图更改“cool.bmp”图像中的像素并将其绘制到更改的窗口中。到目前为止,所有代码都正确执行,但是当我更改 pix 数组中的字节时,图像并没有改变(是的,我正在重绘屏幕)。

   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);
                for(int i = 0; i< 1920*1080*4; i+=4) // problem!! 
                {
                    b[i] = 255;//blue
                    b[i+1] = 255;//green
                    b[i+2] = 255;//red
                    b[i+3] = 255;//alpha
                }

//从位图图像中获取pixArray的方法

BYTE* getPixArray(HBITMAP hBitmap)
{

    HDC hdc,hdcMem;

    hdc = GetDC(NULL);
    hdcMem = CreateCompatibleDC(hdc); 

    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))
    {
        ::printToDebugWindow("FAIL\n");
    }
    // create the bitmap buffer
    BYTE* lpPixels = new BYTE[MyBMInfo.bmiHeader.biSizeImage];

    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(hdcMem, hBitmap, 0, MyBMInfo.bmiHeader.biHeight, (LPVOID)lpPixels, &MyBMInfo, DIB_RGB_COLORS))
    {
        ::printToDebugWindow("FAIL\n");
    }
    return lpPixels;
}

看起来像这样的代码应该将图像中的所有像素都更改为白色?但它没有效果。

BYTE* b = ::getPixArray(hBitmap);
                for(int i = 0; i< 1920*1080*4; i+=4) // problem!! 
                {
                    b[i] = 255;//blue
                    b[i+1] = 255;//green
                    b[i+2] = 255;//red
                    b[i+3] = 255;//alpha
                }
4

1 回答 1

2

GetDIBits()只需将位图复制到缓冲区。SetDIBits()修改缓冲区后,您需要将其设置回 HBITMAP 。

于 2013-06-16T20:00:51.173 回答