Click here to Skip to main content
15,878,316 members
Articles / Programming Languages / C++

LodePngWin32 Class for Rendering PNG Files in Raw WinAPI

Rate me:
Please Sign up or sign in to vote.
4.83/5 (6 votes)
23 Apr 2014CPOL2 min read 19.1K   1.1K   17   4
C++ class that provides a convenient wrapper for lodepng library
Image 1


This project contains a class which provides a wrapper around the lodepng library provided by Lode Vandevenne. Its primary purpose is to provide tools to easily read and display PNG files, for C++ programmers who write in raw WinAPI.


The PNG image format is an excellent format to use for images which are to be included in programs; it provides lossless compression which gives dramatically smaller files than BMP, with far better image quality than JPG. However, the normal WinAPI libraries cannot handle PNG files.

Recently, I was creating a game which used sprite-based graphics; I wrote it initially using the sprite images from the ancient Nethack game; it stored its sprites in a 1.2MB file containing over 1000 sprites! It worked well and was easy to use, but the same file in PNG format was 155KB ... much better!!

I started researching PNG libraries, and discovered Lode Vandevenne's LodePNG library, which comprised a single 6600-line C++ source file and a header. However, I had some problems with it. The first was that he did not have any examples which showed how to access his resulting data from a WinAPI application; all his rendering examples were OpenGL or SDL. Also, it required creating two vector objects for each file, which was awkward when several files were involved.

So, after doing some research online, I found a way to convert his BMP vector into an HBITMAP reference which can be used by BitBlt and other normal WinAPI GDI functions, and that worked quite well. With all the required tools in hand, I then wrapped all these functions into a simple class which makes access to lodepng quite trivial. The enclosed demo program will demonstrate how to use this library.

Using the Code

#include "lode_png.h"

//  instantiating classes for the PNG files
//  When loading multi-image (i.e., sprite) files,
//  specify the individual sprite width and height.
//  tiles32.png contains a 40x27-sprite array of sprite images
#define  SPRITE_WIDTH      32
#define  SPRITE_HEIGHT     32
LodePngWin32 pngSprites("tiles32.png", SPRITE_HEIGHT, SPRITE_WIDTH) ;

//  for single-image file, only filename is required
LodePngWin32 pngFireCat("FireCat.png") ;

typedef struct sprite_sel_s {
   uint column ;
   uint row ;
} sprite_sel_t ;

//  select four arbitrary sprites from the tiles32 image file
static sprite_sel_t const sprite_selection[4] = {
{  6, 13 },   //  shield
{  8, 20 },   //  green gem
{ 23, 14 },   //  multi-colored amulet
{ 39,  0 },   //  smiling cat
} ;

//  For Window-based applications, render images in WM_PAINT.
//  For dialog-based applications, use WM_INITDIALOG
   case WM_PAINT:
      hdc = BeginPaint (hwnd, &ps);
      //  render some sprites
      pngSprites.render_bitmap(hdc,  50, 100, sprite_selection[0].column, sprite_selection[0].row) ;
      pngSprites.render_bitmap(hdc,  50, 200, sprite_selection[1].column, sprite_selection[1].row) ;
      pngSprites.render_bitmap(hdc, 120, 100, sprite_selection[2].column, sprite_selection[2].row) ;
      pngSprites.render_bitmap(hdc, 120, 200, sprite_selection[3].column, sprite_selection[3].row) ;

      //  render a single image
      pngFireCat.render_bitmap(hdc, 200,  50) ;
      EndPaint (hwnd, &ps);
      return 0;


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior) Retired
United States United States
I have worked for 25+ years as a firmware engineer, in various environments. In my private life, I create a variety of applications, including console applications in Linux and Windows, and GUI applications in Windows. All of my programs and code snippets are freeware, with source code available. All programs are written in C or C++, and built using the MinGW toolchain.

Comments and Discussions

QuestionWhy not GDI+? Pin
xFF25522-Jan-15 6:26
professionalxFF25522-Jan-15 6:26 
AnswerRe: Why not GDI+? Pin
Derell Licht30-Jan-15 10:34
professionalDerell Licht30-Jan-15 10:34 
Okay, that's good.
I don't use GDI+, I just code in straight WinAPI, so I needed this solution, which works very nicely.
If you use some other platform that doesn't require it, then you won't need this. Wink | ;)

Later note:
BTW, I don't build my software using Visual C++, I use MinGW... from my initial research, it does not appear that GDI+ can be easily linked to MinGW apps, and even if one gets that working, there are still licensing issues related to distributing the related header files. These are issues that I don't want to deal with, and generally haven't needed to!

modified 30-Jan-15 16:44pm.

GeneralRe: Why not GDI+? Pin
xFF25531-Jan-15 5:06
professionalxFF25531-Jan-15 5:06 
GeneralRe: Why not GDI+? Pin
Derell Licht22-Nov-15 6:54
professionalDerell Licht22-Nov-15 6:54 

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.