Converting PNG to HICON

Loading any PNG resource as an hIcon. Vista introduced PNG encoding for icons. There is an API that will convert PNGs to valid hIcon handles. API Declaration follows:

Private Declare Function CreateIconFromResourceEx Lib "user32.dll" ( _   pbIconBits As Any, ByVal cbSize As Long, ByVal fIcon As Long, _   ByVal dwVersion As Long, ByVal cxDesired As Long, _   ByVal cyDesired As Long, ByVal uFlags As Long) As Long

Simple example.

Let’s say you have the entire PNG image format in a zero-bound byte array, maybe loaded from file or form a resource, or within an actual ICO/CUR file. The API only needs to know where in the byte array that the PNG format begins. No icon directory or other icon header information is required.

hIcon = CreateIconFromResourceEx(pngData(0), UBound(pngData) + 1, 1, &H30000, 0, 0, 0)

Of course, you would destroy the hIcon when it is no longer needed.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

More From DevX