[C++] Pure Win32 Semi transparent PNG?

Jun 4, 2011 at 2:54am
Either research is failing me in this case, or I just don't want to accept the facts of how this works. Probably the second one. Is there a way in Win32 to use semi-transparent PNGs without using a second library? Or is the only way something like DirectX, or openGL?

Basically, I will have a semi-transparent PNG that will need painted once, and after that the BG will never change. (If that helps any) So do I need a second library? If so, what is a modern one that is compatible with XP and up?

Thanks!
Last edited on Jun 4, 2011 at 2:54am
Jun 4, 2011 at 6:37am
I don't do much GUI, but I bet AlphaBlend() (http://msdn.microsoft.com/en-us/library/dd183351(VS.85).aspx) is what you need.
Jun 5, 2011 at 4:48am
Thanks, I'll see if I can make that work, but I'm not sure if I'll be able to due to the fact that my PNG images already have transparency, and it's not even across the entire image.
Jun 5, 2011 at 5:39am
If you read the document, AlphaBlend() has no issues with images with an alpha channel.
Jun 5, 2011 at 6:01am
And that's what I get from not reading all the way to the bottom.

I'll try to let you know how it goes in the next couple days.
Jun 5, 2011 at 11:47am
with ImageList apis and others
Jun 6, 2011 at 10:07pm
Okay, AlphaBlend only works with BMPs that don't have an alpha layer. ImageList will let me use one, but it displays the same. It would be nice if I could just use PNGs, but GDI+ doesn't allow me to use resource images.

Does anybody have a resource that shows how to read and place PNGs with proper transparency?
Jun 6, 2011 at 10:28pm
From MSDN about AlphaBlend:

The SourceConstantAlpha member of BLENDFUNCTION specifies an alpha transparency value to be used on the entire source bitmap. The SourceConstantAlpha value is combined with any per-pixel alpha values. If SourceConstantAlpha is 0, it is assumed that the image is transparent. Set the SourceConstantAlpha value to 255 (which indicates that the image is opaque) when you only want to use per-pixel alpha values.


So it does allow per-pixel alpha. Did you set SourceConstAlpha to 255?
Jun 6, 2011 at 11:10pm
Yes, nothing displays at all if my bitmap has an alpha layer in it.

1
2
3
4
5
6
7
8
SelectObject(bmpHDC, LoadImage(GetModuleHandle(0), MAKEINTRESOURCE(IDB_TEMP), IMAGE_BITMAP, 30, 40, 0));
BLENDFUNCTION ftn={0};
ftn.AlphaFormat=0;
ftn.BlendFlags=0;
ftn.BlendOp=AC_SRC_OVER;
ftn.SourceConstantAlpha=255;

AlphaBlend(hdc, 10, 10, 30, 40, bmpHDC, 0,0, 30, 40, ftn);


Nothing displays using that.
Jun 6, 2011 at 11:27pm
AlphaFormat should be AC_SRC_ALPHA (not zero), and the bitmap needs to have the RGB values pre-multiplied with the alpha channel, and the bitmap must be 32 bits per pixel.
Jun 6, 2011 at 11:49pm
and the bitmap needs to have the RGB values pre-multiplied with the alpha channel


Not sure what that part means, but I changed the other two, still nothing showing.
Jun 6, 2011 at 11:52pm
Jun 7, 2011 at 12:03am
Okay, shouldn't PS have taken care of that when I saved the image?
Jun 7, 2011 at 12:14am
Good question. I don't know. Graphics is not my area. :-(
Jun 7, 2011 at 12:18am
Me neither. :) I'm thinking I'm just going to make myself understand GDI+ streaming so that I can stream from a resource. At least then I'll get my transparency correct.

Edit: GDI+ Can do what is needed. Still working on how to load a PNG from the resource files, but it works perfectly other wise. To future readers, if you're looking how to get a brush to have a transparent PNG, although it calls for an Image class, look into the Bitmap class to load it from the resoruce files.
Last edited on Jun 9, 2011 at 12:50am
Topic archived. No new replies allowed.