Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

c++ - Load a png resource into a CBitMap

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...