Introduction
This is a very simple
Sample Code
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.
Background
I had been searching the
net for a really simple
PNG MFC 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:
PngImage png;
if ( png.load(dlg.GetPathName()) )
{
int width = png.getWidth();
int height = png.getHeight();
unsigned char* data = png.getBGRA();
if (m_bitmap) delete m_bitmap;
if (m_visible) delete m_visible;
m_bitmap = doCreateCompatibleBitmap(width, height, data, this);
m_visible = doZoomBitmap(m_bitmap, this);
Invalidate();
UpdateWindow();
}