Click here to Skip to main content
13,352,157 members (70,156 online)
Click here to Skip to main content
Add your own
alternative version


65 bookmarked
Posted 3 Aug 2005


, 7 Sep 2005
Rate this:
Please Sign up or sign in to vote.
Load and display PNG files in MFC.

Sorry, it should have been PNGView.jpg


This is a very simple approach to display PNG files in MFC. Two common libraries provide the needed functionality: zlib and libpng. These libraries are included in the source file.


I had been searching the net for a really simple PNG example for a whole while. But all I found were C files that contained more preprocessor directives than keywords. I don't like preprocessor directives so I've written this example. It uses only one class to do the whole stuff.

Using the code

At first:

You might have to upgrade your include-folder settings, because libpng wants to know where your zlib.h is located. After you have unzipped the downloaded archive, you can find this file in the ...\zlib\code\ folder.

The FileOpen handler:

This chunk of code works off both reading and showing the PNG file:

// create temporary object
PngImage png;

// import png file
if ( png.load(dlg.GetPathName()) )
    // get size
    int width  = png.getWidth();
    int height = png.getHeight();

    // get blue, green, red and alpha values
    unsigned char* data = png.getBGRA();

    // free memory first
    if (m_bitmap) delete m_bitmap;
    if (m_visible) delete m_visible;

    // create a CBitmap to display
    m_bitmap = doCreateCompatibleBitmap(width, height, data, this);

    // used to speed up OnPaint()
    m_visible = doZoomBitmap(m_bitmap, this);

    // request graphical update


Version 1.3


  • Reading 1, 2 and 4 bit monochrome images.
  • Reading 1, 2 and 4 bit monochrome images with transparency.
  • Reading images that use a palette.

Version 1.2


  • Displaying the CBitmap object got faster.

Version 1.1

Not submitted

Version 1.0


  • Reading 8-bit RGB images.
  • Reading 8-bit RGB images with transparency.
  • Reading 8-bit monochrome images.
  • Reading 8-bit monochrome images with transparency.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Achim Klein
Web Developer
Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionTechnical issues Pin
Member 124067262-May-16 23:53
memberMember 124067262-May-16 23:53 
GeneralWhy not use CPngImage in mfc? it's convinience and efficient Pin
ecjtuxsyuan13-Nov-10 7:27
memberecjtuxsyuan13-Nov-10 7:27 
GeneralRe: Why not use CPngImage in mfc? it's convinience and efficient Pin
Norbert Bons1-Sep-14 5:14
memberNorbert Bons1-Sep-14 5:14 
QuestionHow to display a transparent PNG? Pin
Nvng YA25-Apr-08 7:57
memberNvng YA25-Apr-08 7:57 
AnswerRe: How to display a transparent PNG? Pin
benjamin2311-May-09 2:53
memberbenjamin2311-May-09 2:53 
General16bits (hicolor support) + license question Pin
tcm199825-Sep-06 6:32
membertcm199825-Sep-06 6:32 
GeneralRe: 16bits (hicolor support) + license question Pin
Achim Klein3-Oct-06 3:14
memberAchim Klein3-Oct-06 3:14 
Questionincorrect source Pin
jalal_haddad1-May-06 9:33
memberjalal_haddad1-May-06 9:33 
AnswerRe: incorrect source Pin
Achim Klein2-Jun-06 4:14
memberAchim Klein2-Jun-06 4:14 
AnswerRe: incorrect source Pin
heshule22-Mar-07 23:36
memberheshule22-Mar-07 23:36 
GeneralCrashes under Win32 Pin
Achim Klein10-Sep-05 4:27
memberAchim Klein10-Sep-05 4:27 
Hi folks,

today libpng crashes several times on my system (MFC project).

The reason was:
png_default_warning() tries to send messages by fprintf.

MFC solution:
- open your pngconf.h file
- search and enable: #define PNG_NO_CONSOLE_IO

Achim Klein

We can do no great things, only small things with great love. - Mother Theresa
NewsUpdate Pin
Achim Klein27-Aug-05 8:38
memberAchim Klein27-Aug-05 8:38 
GeneralSaving to PNG format Pin
Jinxter8-Aug-05 4:09
memberJinxter8-Aug-05 4:09 
GeneralRe: Saving to PNG format Pin
Achim Klein9-Aug-05 4:08
memberAchim Klein9-Aug-05 4:08 
GeneralSize of Code Pin
ReorX4-Aug-05 0:44
memberReorX4-Aug-05 0:44 
GeneralRe: Size of Code Pin
Kippesoep4-Aug-05 5:34
memberKippesoep4-Aug-05 5:34 
Questionalpha chanel support? Pin
AYA2WORK3-Aug-05 23:52
memberAYA2WORK3-Aug-05 23:52 
AnswerRe: alpha chanel support? Pin
Achim Klein4-Aug-05 15:10
memberAchim Klein4-Aug-05 15:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 7 Sep 2005
Article Copyright 2005 by Achim Klein
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid