3
votes

I have an icon with partial alpha (alpha values between 0 and 255) that I want to display using GDIPlus. When using the Bitmap constructor of GDI+ that takes the direct filename, the file displays properly. However, when loading from resource, it has a problem recognizing alpha. I looked on MSDN, and there are problems with alpha: http://msdn.microsoft.com/en-us/library/windows/desktop/ms536318.aspx. By retrieving the ICONINFO structure from the Icon, I can get rid of the fully transparent pixels, however, the partially transparent pixels still appear either as fully opaque or fully transparent.

I wanted to know how to create a Win32 Bitmap from an Icon in resource with the partial alpha values.

2
Show some code. No probs at all loading partially transparent icons from resources.David Heffernan
/*Loaded and Locked resource*/ HICON icon = LoadIcon (modhandle, MAKEINTRESOURCE(_wtoi(vImageName))); bitmap = Gdiplus::Bitmap::FromHICON(icon); The bitmap thus loaded, has either zero or full alpha, and no partial alphas are seenKuldip Krori
I always use LoadImage since it gives you control over icon sizes. But they both have no probs with partial alpha. Apps routinely display standard IDI_xxx icons with alpha by calling LoadIcon. The problem occurs after the call to LoadIcon.David Heffernan
The icons I am using are not the standard IDI_xxx icons (it may be the case that the IDI_xxx icons dont have partial alpha, I am not sure on that though), but custom icons included in the resource. There is a reported community addition bug reported on MSDN too (see the "Community Additions" msdn.microsoft.com/en-us/library/windows/desktop/…), not sure I am facing a problem due to this or something elseKuldip Krori
I load my own custom icons with LoadIcon and LoadImage. No probs at all. IDI_xxx do have partial alpha too. Your problem is elsewhere.David Heffernan

2 Answers

3
votes

You can use LoadResource to get a pointer to the icon and and its image data. You can pass the pointer to the image data to the appropriate Bitmap constructor. This is a bit of a chore because icons have a peculiar resource format.

If possible, it would be simpler to store your image as a transparent (i.e. 32bpp argb) bitmap. In this case you can use LoadImage with LR_CREATEDIBSECTION.

Update

Apparently LoadIcon does load the alpha correctly. It would appear that the problem is GdiPlus not respecting the alpha when you construct a GdiPlus::Bitmap from an HICON. What you could do is:

  • Use LoadIcon to load the icon.
  • Use GetIconInfo to get the ICONINFO. hbmColor is the handle of the transparent bitmap.
  • Use GetDIBits to get the bitmap bits from hbmColor.
  • Pass the data to the Bitmap constructor that takes bits and understands alpha.
1
votes

The alpha channel is disturbed after you call LoadIcon. The Win32 APIs that load icons, e.g. LoadIcon, LoadImage, etc. are well proven. They reliably load icons with partial alpha.

You need to investigate the code that executes after the icon has been loaded. I can't give you a solution or an explanation, but I am confident that LoadIcon is not the culprit.

I wanted to know how to create a Win32 Bitmap from an Icon in resource with the partial alpha values.

Call GetIcon or GetImage to obtain an HICON. Then call GetIconInfo. The bitmap you need is in the hbmColor field of the ICONINFO struct.