October 22, 2024
Chicago 12, Melborne City, USA
C++

Is this an error in the documentation of TransparentBlt()?


Microsoft’s documentation of the GDI function TransparentBlt() states:

The TransparentBlt function supports all formats of source bitmaps. However, for 32 bpp bitmaps, it just copies the alpha value over.

Is this an error?

The code below contradicts that documentation by demonstrating that TransparentBlt() zeroes out the Alpha channel value for the non-transparent pixel, and does not copy any channels of the source transparent pixel at all (as it shouldn’t):

#include <windows.h>

void Test_TransparentBlt(void)
{
    BITMAPINFO bmi = { {sizeof(BITMAPINFOHEADER), 2, 1,1,32,BI_RGB,0,0,0,0,0} };
    PDWORD PixelsSrc;
    HBITMAP hBmpSrc = CreateDIBSection(0, &bmi, DIB_RGB_COLORS, (PVOID*)&PixelsSrc, NULL, 0);
    PixelsSrc[0] = 0xEE555555; PixelsSrc[1] = 0xFFAAAAAA;

    PDWORD PixelsDst;
    HBITMAP hBmpDst = CreateDIBSection(0, &bmi, DIB_RGB_COLORS, (PVOID*)&PixelsDst, NULL, 0);
    PixelsDst[0] = 0xCC222222; PixelsDst[1] = 0xDD333333;
    
    HDC hDCsrc = CreateCompatibleDC(NULL);
    HDC hDCdst = CreateCompatibleDC(NULL);
    SelectObject(hDCsrc, hBmpSrc);
    SelectObject(hDCdst, hBmpDst);
    
    printf("Src: %08X, %08X\n", PixelsSrc[0], PixelsSrc[1]);    
    printf("Dst: %08X, %08X\n", PixelsDst[0], PixelsDst[1]);
    TransparentBlt(hDCdst, 0, 0, 2, 1, hDCsrc, 0, 0, 2, 1, 0x00555555);
    printf("DST: %08X, %08X\n", PixelsDst[0], PixelsDst[1]);
}

The output is:

Src: EE555555, FFAAAAAA
Dst: CC222222, DD333333
DST: CC222222, 00AAAAAA

If the source Alpha value for the non-transparent pixel had been copied over, then the output would have been:

Src: EE555555, FFAAAAAA
Dst: CC222222, DD333333
DST: CC222222, FFAAAAAA

Note: the Alpha channel is encoded in the MSB of the pixel data (bigendian DWORD).



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video