Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load a png resource into a CBitMap

Tags:

c++

mfc

How do I load a png resource into a CBitMap? When I try this it doesn't seem to work:

CImage image;
image.LoadFromResource(AfxGetInstanceHandle(), IDB_PNG1);
bitmap.Attach(image.Detach());

It gives me an error resource type not found. Is there any other way to load a PNG resource?

like image 448
Rohit Sasikumar Avatar asked Jun 09 '10 12:06

Rohit Sasikumar


1 Answers

If you are using VS2008 or newer (MFC Feature Pack) you can use CPngImage which derives from CBitmap and is contained in <afxtoolbarimages.h>. CPngImage is an internal class:

The following classes are used internally in MFC. For completeness, this section describes these internal classes, but they are not intended to be used directly in your code.

If you want to use CPngImage you need to use the resource type "PNG":

#define AFX_PNG_RESOURCE_TYPE  _T("PNG")

Example usage

CPngImage image;
image.Load(IDB_PNG1, nullptr);
bitmap.Attach(image.Detach());
like image 169
hofingerandi Avatar answered Sep 24 '22 02:09

hofingerandi