Click here to Skip to main content
15,884,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I have some code that uses GDI+ to convert an icon to grayscale*. However, when transparency (i.e. alpha channel) is involved, the resulting icon seems to use pre-multiplied alpha - rather than normal alpha - making the transparent pixels look darker.


* Create GDI+ Bitmap from HICON, create blank Bitmap, draw icon bitmap onto new bitmap using a colour matrix, convert new bitmap to HICON.

What I have tried:

I found that when drawing to the screen using GDI+ Graphics class, it looks ok. It's only when converting back to an icon using Bitmap::GetHICON that the problem occurs. Which leads me to believe that the GetHICON function is implicitly converting the image to pre-multiplied alpha.

It does not seem to matter whether I use PixelFormat32bppARGB or PixelFormat32bppPARGB at any point, as I get the same effect.
Posted
Updated 18-Nov-18 18:34pm
v2
Comments
Aydin Homay 19-Nov-18 3:24am    
Please post your code and explain on the code what you are trying to do
XTAL256 20-Nov-18 17:42pm    
The code is quite complex, and it will be hard to make a simple example to demonstrate my problem.

However, since you prompted me to look at the code again, I think I see what the problem is. The code that converts HICON to GDI+ Bitmap tries to do some tricky stuff with the alpha channel of 32-bpp icons.

Specifically, my code is calling Bitmap::FromHBITMAP and passing the icon's colour bitmap (obtained from GetIconInfo). It then copies the pixel data to the GDI+ bitmap.
If I call Bitmap::FromHICON instead, then the 8-bit alpha channel is not preserved and the 1-bit mask is used instead.

I will investigate that code further.
Michael Haephrati 18-May-19 9:34am    
I think you should call SetColorKey with the correct transparency
0x01AA 5-Jan-24 20:10pm    
Please explain what 'pre-multiplied alpha' means.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900