Skip navigation

How do you load a PNG image in C++?

Well, most people use libpng

But WAIT!! That library is NSFDU. “NOT SAFE FOR DIRECT USE” – Yes i just made that up, but its absolutely true.

The API is AWFUL. If you don’t believe me, spend a day with it, struggle through it, THEN check out OpenIL

In less than 50 lines, I’m loading an image and looking at its raw data

Compare with bare minimal libpng direct use example. This is an example of how DIFFICULT using libpng directly is.

DevIL’s BRILLIANT API mimics OpenGL’s state-machine approach to image handling. Its fantastic, and for anyone who’s ever used OpenGL a bit at least, picking it up will be a breeze and everything is as you think (hope?) it should be.

#include <stdio.h>
#include <stdlib.h>

#undef _UNICODE
#include "il.h"

#pragma comment( lib, "DevIL.lib" )

// Wow. DevIL is amazing.

// From

// The library consists of three sub-libraries:
//  * IL - main DevIL library. It allows you to load and save images to files. Every function in this library have 'il' prefixed to their name.
//  * ILU - this library contains functions for altering images. Every function in this library have 'ilu' prefixed to their name.
//  * ILUT - this library connects DevIL with OpenGL. Every function in this library have 'ilut' prefixed to their name. 

int main()
  printf("DevIL has been initialized\n");
  // Loading an image
  ILboolean result = ilLoadImage( "4px.png" ) ;

  if( result == true )
    printf("the image loaded successfully\n");
    printf("The image failed to load\n" ) ;
    ILenum err = ilGetError() ;
    printf( "the error %d\n", err );
    printf( "string is %s\n", ilGetString( err ) );

  int size = ilGetInteger( IL_IMAGE_SIZE_OF_DATA ) ;
  printf("Data size:  %d\n", size );
  ILubyte * bytes = ilGetData() ;

  for( int i = 0 ; i < size; i++ )
    // see we should see the byte data of the image now.
    printf( "%d\n", bytes[ i ] );

Download the code (with DevIL binaries included!) on esnips! (thanks esnips!)



    • rsz
    • Posted February 6, 2011 at 11:09 pm
    • Permalink

    Ha, and it took me 10 minutes to read this tip and get DevIL(OpenIL) to write me a PNG in 4 lines on Windows or Linux. Brilliant!

    ILubyte *rgba = new ILubyte[nx*ny*4];
    ilTexImage(nx, ny, 1, 4, IL_RGBA, IL_UNSIGNED_BYTE, rgba);

  1. Thanks mate! Been looking for something easier to use than pnglib. This helps bunches.

  2. Thanks for the help brother ….!

    • Erik
    • Posted April 18, 2012 at 8:50 pm
    • Permalink

    i get linker errrors although i linked the DevIL.lib
    i used visual studio 2010 and code::blocks both didn´t work.

2 Trackbacks/Pingbacks

  1. By In C++ PNG statt BMP laden on 29 May 2010 at 3:26 pm

    […] gefunden habe: The Chaos Rift • View topic – C++ OpenGL+LibPNG – Loading a PNG easily How to load a PNG image in C++ Webseiten: http://www.kirchengemeinde-aldingen.de Tutorials: LAMPP […]

  2. […] found this tutorial, but when I copy code to my C++ project, there are many […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: