Anthony Raimondo
Anthony Raimondo

Reputation: 1721

c++ changing the values in a bitmap

Im trying to change the pixels in the "cool.bmp" image and draw it to a window altered. so far all the code executes correctly but when i change the bytes in the pix array the image does not change (yes im redrawing the screen).

   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
                }

//method to get pixArray from bitmap image

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;
}

the code ontop that looks like this should change all the pix in the image to white? but it has no effect.

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
                }

Upvotes: 2

Views: 2151

Answers (1)

Gabriele Giuseppini
Gabriele Giuseppini

Reputation: 1581

GetDIBits() just copies the bitmap to the buffer. You need to set it back into the HBITMAP with SetDIBits() after modifying the buffer.

Upvotes: 2

Related Questions