fireman
fireman

Reputation: 193

HBITMAP StretchBlt caused image saturation

I used the following code to resize win32 HBITMAP.

HBITMAP ResizeBitmap(HDC hDC, HBITMAP source)
{
    HDC hMemDC2 = CreateCompatibleDC(hDC);
    HGDIOBJ hOld2 = SelectObject(hMemDC2, source);

    BITMAP bitmap = { 0 };
    GetObject(hBitmap, sizeof(BITMAP), &bitmap);

    float SCALE = IMAGE_SCALE;
    int nWidth = (int)ceil(bitmap.bmWidth / SCALE / 32) * 32;
    int nHeight = (int)ceil(bitmap.bmHeight / SCALE / 32) * 32;
    int dWidth = (int)round(bitmap.bmWidth / SCALE);
    int dHeight = (int)round(bitmap.bmHeight / SCALE);

    HDC hMemDC1 = CreateCompatibleDC(hDC);
    HBITMAP hBitmap1 = CreateCompatibleBitmap(hDC, nWidth, nHeight);
    HGDIOBJ hOld1 = SelectObject(hMemDC1, hBitmap1);

    StretchBlt(hMemDC1, 0, 0, dWidth, dHeight, hMemDC2, 0, 0, bitmap.bmWidth, bitmap.bmHeight, SRCCOPY);

    SelectObject(hMemDC1, hOld1);
    SelectObject(hMemDC2, hOld2);
    DeleteDC(hMemDC1);
    DeleteDC(hMemDC2);

    return hBitmap1;
}

However, I found the new image was saturated. As show bellow the original image enter image description here

And the new image (Please ignore the green rectangle in the image)

enter image description here

Why?

Upvotes: 2

Views: 318

Answers (0)

Related Questions