A previous article demonstrates how easy is to load images from PNG resources, by using Direct2D MFC support. I have found even an easier way: CPngImage class which extends CBitmap with methods that allow loading images from PNG format files and resources.
Here is a brief example:
Using CPngImage class for loading PNG resources
1 2 3 4 5 6 7 |
class CDemoDlg : public CDialogEx { // ... CPngImage m_imageDemo; CStatic m_staticPicture; // ... }; |
1 2 3 4 5 6 7 |
BOOL CDemoDlg::OnInitDialog() { // ... VERIFY(m_imageDemo.Load(IDB_PNG_PENCIL)); m_staticPicture.SetBitmap(m_imageDemo); // ... } |
Demo project
It is a simple dialog-based MFC application that loads a bitmap from PNG resource then use the bitmap for setting the image in a static picture control.
Download: PNG Resource Loading (VS 2015).zip (1899)
Notes
- of course, we can follow a similar way to set images in other controls, like for example CMFCButton;
- CPngImage was designed for internal use in the MFC framework, but so far I didn’t see any problem in using it for our own purposes.
Resources and related articles
- MSDN: CPngImage Class
- Codexpert: Easy PNG Resource Loading with MFC – Part 1
- IconArchive: Pencil Icons by RecluseKC