Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Transparent Bitmap with GDI?

I want to implement a layering system in my application and was thinking of creating a bunch of transparent bitmaps, adding content to them then blitting them on top of each other, how can this be done without setting each pixel to (0,0,0,0). I'm using Pure win32, not MFC, thanks.

like image 227
jmasterx Avatar asked Apr 28 '10 03:04

jmasterx


People also ask

How does GDI work with bitmapping?

By "selecting" a bitmap into a memory DC, the DC then represents that bitmap as a drawing surface, and all the normal GDI operations can be performed on the bitmap.

What is transparency in GDI+ color?

Sometimes we need to draw objects on top of images- and these objects may need to be transparent. As we discussed earlier, color in GDI+ has four components: alpha, red, green, and blue. The value of each component varies from 0 to 255. The alpha component represents the transparency in GDI+ color.

How to make a bitmap image transparent?

Will make white (255,255,255) transparent on any bitmap you use this image attribute with. The simplest solution is to use some format other than BMP. You need the bits to contain alpha data, and you need the Bitmap to be in a format that has alpha data.

Why doesn't GDI+ use the alpha channel of a BMP?

When you load a BMP with GDI+, it will always use a format without alpha, even if the BMP has an alpha channel. I believe the data is there in the image bits, but it's not being used.


1 Answers

What do you mean by transparent?

If you are looking for partial (to full) transparency, then AlphaBlend is the GDI API to use. Loading bitmaps with alpha is tricky - The only format the base windows API supports for loading bitmaps with alpha is a 32bpp .BMP file with an alpha channel in the top 8 bits of each byte - and the lower bytes should to be pre-multiplied.

It is possible to use GDI+ to load a variety of image formats with alpha - PNG is probably the best to go for, and blit onto a 32bpp DIBSection so you can use AlphaBlend and plain-old GDI functions.

If you want a simple transparency mask rather than a full alpha channel you can use TransparentBlt along with a color key to mask out areas of a bitmap when blitting it.

like image 115
Chris Becke Avatar answered Sep 20 '22 01:09

Chris Becke