tags:

views:

213

answers:

2

I have a PNG-encoded icon as a byte array in memory. What is the recommended way of creating an HICON object from this byte array?


Imaginary bonus points if you know a solution without ATL or GDI+... :)

A: 
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, dataSize);
LPVOID pImage = GlobalLock(hMem);
memcpy(pImage, pngData, dataSize);
GlobalUnlock(hMem);

ATL::CComPtr<IStream> pStream;
CreateStreamOnHGlobal(hMem, TRUE, &pStream);

Gdiplus::Bitmap *pBitmap = new Gdiplus::Bitmap(pStream);
HICON YOUR_HICON = pBitmap->GetHICON();
Ivan Krechetov
also is there any way of doing it without ATL or GDI+?
Epaga
Corrected: now you do have HICON. Without ATL or GDI+ — don't know Epaga, I'm not that hardcore anymore.
Ivan Krechetov
I don't see an Icon class in GDI+ - instead it would work with Gdiplus::Bitmap and then GetHICON.
Epaga
Right! Of course. Icon is in .NET, but not in GDI+. Sorry. Yes, Gdiplus::Bitmap and then GetHICON should work.
Ivan Krechetov
A: 

It looks like you could do this with CreateBitmap and CreateIconIndirect, or maybe even just CreateIcon. Don't ask me for code because I'm not really familiar with this low-level graphics stuff.

Luke