Alex Flint
Alex Flint

Reputation: 6738

create HICON from memory on windows

I have the contents of a .ico file in memory as a const char*, and I wish to create a HICON from this data. My current approach is to write the data to a temporary file and then use LoadImage. Is it possible to instead create the icon directly from memory?

Upvotes: 0

Views: 2448

Answers (2)

tenuki
tenuki

Reputation: 181

With CreateIcon you can certainly create icons but I'm not really sure you can feed it with a png-format image data.

I had success with CreateIconFromResourceEx (sorry for using other language, but take it as an example, you can use that function directly in C) :

from ctypes import *
from ctypes.wintypes import *
CreateIconFromResourceEx = windll.user32.CreateIconFromResourceEx

size_x, size_y = 32, 32 LR_DEFAULTCOLOR = 0

with open("my32x32.png", "rb") as f:
    png = f.read()
hicon = CreateIconFromResourceEx(png, len(png), 1, 0x30000, size_x, size_y, LR_DEFAULTCOLOR)

Hope it helps you.

Upvotes: 3

Anders
Anders

Reputation: 101756

If you want to do this without using GDI+ nor WIC then you have to do some parsing yourself because the format of a .ICO file is not exactly the same as a icon resource in a .EXE/.DLL so you cannot use the resource based icon functions. The official binary spec can be found here and there is a great blog series about it here.

If your .ico file has more than one image in it then you must loop over the icon directories until you find a image dimension you are happy with. You can then call CreateIcon to create a HICON.

Upvotes: 2

Related Questions