2
votes

I started with a PNG image. I then split the alpha channel into a grey scale BMP file and converted the PNG into a BMP. I would like to load both BMP files and merge them to give a HBITMAP with an alpha channel:

HBITMAP splash = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_SPLASH));
HBITMAP splashMask = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_SPLASH_MASK));
HBITMAP splashAlpha = ....

I found an example of creating a HBITMAP directly from a PNG. It uses IStream and COM to do the import. I'd rather not include more dependencies. Surely there is a better way to do this?

1

1 Answers

1
votes

If you're looking for minimum code, you should consider converting your PNGs into a 32-bit bitmap and draw them using the AlphaBlend API.

BLENDFUNCTION fn;
ZeroMemory(&fn, sizeof(fn));
fn.BlendOp = AC_SRC_OVER;
fn.BlendFlags = 0;
fn.SourceConstantAlpha = 255;
fn.AlphaFormat = AC_SRC_ALPHA;
AlphaBlend(dstDC, dstX, dstY, dstW, dstH, hdcSrc, srcX, srcY, srcW, srcH, fn);

The gotcha is that hdcSrc should refer to a 32-bit BGRA image where the Alpha channel is premultiplied into the BGR channels, i.e.

B = B * A / 255;
G = G * A / 255;
R = R * A / 255;