Introduction
This
article shows
how the
GDI
function
AlphaBlend
is used to display and
overlay 32 bit bitmaps with
an
alpha channel. It
is mainly a hint, which
would have saved me hours of
research.
Code Snippet
Collapse
Copy Code
BITMAP bm;
GetObject(background, sizeof(BITMAP), &bm);
AlphaBlend(hdcMemWork, 0,0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0,
bm.bmWidth, bm.bmHeight, blendFkt)
SelectObject(hdcMem, hbmOld);
if(bmInfo.bmiHeader.biBitCount == 32 )
{
for (int y=0; y<bmInfo.bmiHeader.biHeight; ++y)
{
BYTE *pPixel= (BYTE *) lpDIBBits + bmInfo.bmiHeader.biWidth * 4 * y;
for (int x=0; x<bmInfo.bmiHeader.biWidth ; ++x)
{
pPixel[0]= pPixel[0]*pPixel[3]/255;
pPixel[1]= pPixel[1]*pPixel[3]/255;
pPixel[2]= pPixel[2]*pPixel[3]/255;
pPixel+= 4;
}
}
}
Points of interest
I played around a long time
until
AlphaBlend
began working. At least the
trick was to pre multiply
the color values of the
bitmap with the
alpha value.
Then, everything worked
fine. Be careful with the
width and height you provide
to
AlphaBlend()
.
If this exceeds the bounds
of the bitmap, nothing is
displayed. I prefer getting
the width and height
directly out of the bitmap
data as shown.