Displaying PNG images in MFC project using CxImage Library

Microsoft Visual studio does not allow you to include the PNG image in the resource. It gives the option to include only bmp images.

To display the PNG images we need to go for other third party libraries such as CxImage library, gdiplus, ATL class CImage etc. Among these methods CxImage is the simplest one.

We will now see how we can display PNG image in a picture control in MFC using CxImage library by the following steps:-

1. First we need to download the CxImage Library from the given link http://www.codeproject.com/KB/graphics/cximage.aspx. It contains many projects such as CxImage, png, tiff, jpeg etc.

2. Then we need to include all the files used in CxImage project in our project.

3. Also require the cximage.lib, png.lib & zlib.lib generated by the corresponding project given in the downloaded folder to display the png image.

4. Now create a MFC dialog based application & take a picture control in it.

5. Now in the Dialog class of the project include a header file “ximage.h” & create an object of the CxImage class as:

CxImage m_image;

6. Now in the OnInitDialog function we can load the PNG image from the disk on to the CxImage object using the Load method of the CxImage Library.

m_image.Load("PNG image path",CXIMAGE_FORMAT_PNG);

7. Now in the end, on the OnPaint function we can use this CxImage object to draw the png image on our picture control.

CWnd* pWnd = GetDlgItem(IDC_PIC_CTL); //-- IDC_PIC_CTL is the ID of pur picture control.
HWND hWnd = pWnd->GetSafeHwnd();
HDC hdc = ::GetDC(hWnd);
m_image.Draw(hdc, 0, 0, 100, 100); //-- Draw method of CxImage to draw on any control by passing its HDC & the rect on which the PNG image is to be drawn.
150 150 Burnignorance | Where Minds Meet And Sparks Fly!