2

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

Community
  • 1
  • 1
Loom
  • 9,768
  • 22
  • 60
  • 112
  • you can try libpng: http://gnuwin32.sourceforge.net/packages/libpng.htm or this: http://www.codeproject.com/Articles/3537/Loading-JPG-PNG-resources-using-GDI – Ferenc Deak Jun 22 '12 at 08:41

1 Answers1

3

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.

Esme Povirk
  • 3,004
  • 16
  • 24
  • @VincentProvirk How to write resource data from from resource pointer to istream ? –  Jun 12 '16 at 07:26