Loom
Loom

Reputation: 9986

Loading png-file from resources (without MFC, ATL)

I want to load PNG-file from resources. There is a roughly MFC-way (with CResourceStream):

CImage img;
CResourceStream str(0, MAKEINTRESOURCE(id), _T("PNG"));
img.Load(&str);
CBitmap *bmp(CBitmap::FromHandle(img.operator HBITMAP());

But a project is MFC-less and ATL-less. How to write to load png in nonMFC style? As I understand it, this purpose can be achieved by GDI+

EDIT: There is an appropriate implementation of loading png to stream from the answer

Upvotes: 2

Views: 5508

Answers (1)

Esme Povirk
Esme Povirk

Reputation: 3164

Well, GDI+ can easily create an HBITMAP from PNG data in an IStream or file, but going from a resource to an IStream takes some work.

If you call CreateStreamOnHGlobal(NULL, TRUE, &stm), where stm is an IStream* variable, it will basically give you a temporary in-memory stream. You can use FindResource, LoadResource, LockResource, and SizeofResource to get a pointer to your resource data and its size. Once you have both of those things, you can use IStream::Write to copy the data from your resource into the IStream.

Once you have an IStream with your PNG data, pass the IStream to the GDI+ Bitmap constructor, and use the GetHBITMAP method to get an HBITMAP.

Upvotes: 3

Related Questions