Displaying Icons stored as resources with alpha using GDIPlus (WIn32 C++)

二次信任 提交于 2019-12-06 03:40:17

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!