【问题标题】:c++ changing the values in a bitmapc ++更改位图中的值
【发布时间】:2013-06-12 19:00:18
【问题描述】:

我正在尝试更改“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
                }

【问题讨论】:

    标签: c++ windows image winapi bitmap


    【解决方案1】:

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 2020-09-16
    • 2017-04-21
    • 1970-01-01
    • 2014-01-16
    相关资源
    最近更新 更多