Click here to Skip to main content
14,027,802 members
Click here to Skip to main content
Add your own
alternative version


169 bookmarked
Posted 1 Oct 2001

Load, show and convert miscellaneous file-formats using freeimage

, 1 May 2002
Rate this:
Please Sign up or sign in to vote.
Shows the usage of the freeimage-library to show and convert various file-formats

Sample Image


In this article I want to show the following:

  • load, convert and save various file formats (tif, bmp, jpeg, ico, jif, jfif, koa, pcd, pcx, png, pbm, pgm, ppm, ras, targa, tga)
  • supporting multipage-images
  • convert color-model (1bit ... 256 colors ... 16 bit ... 24 bit ... 32 bit)
  • tile the images in horizontal or vertical order
  • integrating freeimage in own projects
  • a sample mdi application with some simple techniques like
    a file-open-box for selecting multiple files
    background-threads for long-duration-tasks (e.g. loading images)
  • zooming and scrollbars
  • improved drawing-speed (using the clipping region)
  • drag/drop from explorer

The Graphics-Suite

Opening and Saving

These functions are implemented using freeimage (see below). The code for opening and saving looks is very simple and looks like this (simplified):

BOOL CGraphicSuiteDoc::OnOpenDocument(LPCTSTR lpszPathName) 

    fif = FreeImage_GetFIFFromFilename(lpszPathName);
    if( fif != FIF_UNKNOWN )
        m_handleFI = FreeImage_Load(fif, lpszPathName);
    return (m_handleFI != NULL);

void CGraphicSuiteDoc::OnFileSaveAs() 
    CString           filter,
    DWORD             dwFlags = OFN_OVERWRITEPROMPT | 
                                OFN_EXPLORER | 
                                OFN_LONGNAMES | 
                                OFN_PATHMUSTEXIST | 
    BOOL              bErg;

    CFileDialog    dlg(FALSE, NULL, szDest, dwFlags, filter, 

    if(  dlg.DoModal() != IDOK)
    fif = FreeImage_GetFIFFromFilename(dlg.GetPathName());
    if( fif == FIF_UNKNOWN )
        AfxMessageBox("Unknown type.");

    bErg = FALSE;
        bErg = FreeImage_Save(fif, m_handleFI, dlg.GetPathName());
    if( !bErg )
        AfxMessageBox("Failed saving !");

Display and Printing

The MDI-sample uses freeimage to do all the image-stuff, except the display. In this sample the display is handled using the API-function StretchDIBits. In the real-code also the clip-box is used in order only to extract the pixels that have to be repainted. Thus the code shown here is a little simplified to the the technique. I've choosen mapping-mode MM_HIMETRIC, because then you can print the images using the correct resolution and metrics.

void CGraphicSuiteView::OnDraw(CDC* pDC)
    CGraphicSuiteDoc    *pDoc = GetDocument();

    double dpiX   = FreeImage_GetDotsPerMeterX(pDoc->m_handleFI),
           dpiY   = FreeImage_GetDotsPerMeterY(pDoc->m_handleFI),
           width  = FreeImage_GetWidth(pDoc->m_handleFI),
           height = FreeImage_GetHeight(pDoc->m_handleFI),
           sizeX,    // 1/100 mm == HIMETRIC
    BYTE    *pData    = FreeImage_GetBits(pDoc->m_handleFI);

    if( dpiX==0.0 )    dpiX = 72.0 * 100.0 / 2.54;
    if( dpiY==0.0 )    dpiY = 72.0 * 100.0 / 2.54;

    sizeX = m_Zoom * 100.0 * 1000.0 * width  / dpiX;
    sizeY = m_Zoom * 100.0 * 1000.0 * height / dpiY;

    // stretched 
        // x-y-coord of destination upper-left corner
        sizeX, -sizeY,                       
        // width-height of destination rectangle
        (int)(sizeX+0.5), -(int)(sizeY+0.5), 
        // x-y-coord of source upper-left corner
        0, 0,                                
        // width-height of source rectangle
        (int)(width+0.5), (int)(height+0.5), 
        pData,                               // bitmap bits
        FreeImage_GetInfo(pDoc->m_handleFI),    // bitmap data
        DIB_RGB_COLORS,                      // usage options
        SRCCOPY                              // raster operation code

.. or use (see code for how the parameters are to be initialized)

    // no stretching
        pDC->m_hDC, // handle to DC
        xDst, yDst, // x-y-coord of destination upper-left corner
        dxSrc, dySrc, // width-height of source rectangle
        xSrc, ySrc, // x-y-coord of source upper-left corner
        0,//uStartScan,// first scan line in array
        FreeImage_GetHeight(pFIBitmap), // number of scan lines
        FreeImage_GetBits(pFIBitmap), // array of DIB bits
        FreeImage_GetInfo(pFIBitmap), // bitmap information
        DIB_RGB_COLORS); // RGB or palette indexes

Image information

To complete the program, the relevant image information is shown in a dialog:


(Text taken from the freeimage-introduction ! )

What is freeimage ?

FreeImage is an Open Source library project for developers who would like to support popular graphics image formats like BMP, JPEG, TIFF and PCX and others as needed by today's multimedia applications. The libraries comes in two versions: a binary distribution that can be linked against any 32-bit c++ compiler and a source distribution. Workspace files for Microsoft Visual C++ 6 are provided, as well as makefiles for linux.

Features of freeimage

  • Ease of use. The library has been designed to be extremely simple in use. Our motto is: make difficult things simple instead of simple things difficult.
  • Not limited to the local PC. The unique FreeImageIO structure makes it possible to load your images from virtually anywhere. Possibilities include standalone files, memory, cabinet files and the Internet, all this without recompiling the library!
  • Plugin driven. The internal engine is made completely modular using a clever plugin system. Easily Write new plugins and store them in DLL files or embed the plugins directly into your application!
  • Color conversion. FreeImage provides many functions to convert a bitmap from one bitdepth to another.
  • Directly access bitmap bits and palette. Functions are provided which allow you to directly access the bitmap palette (if available) and bitmap bits.
  • Written in portable C++, should compile on all 32 bit Windows systems as well as under Linux and Solaris. Full source code provided.
  • Open Source Dual-License. You can choose the license that has the most advantages for you: Use the liberal FreeImage Public License to use FreeImage commercially or the GNU General Public License to use FreeImage into your open source project.
  • Easily integrates into DirectX and OpenGL. Only a minimum of programming is necessary to store a FreeImage bitmap into a DirectDraw surface. Or use FreeImage to load your Direct3D/OpenGL textures.
  • Provides many test programs to "show-off" the library.
  • Supports many formats, such as:
    • BMP files [reading, writing]
    • Dr. Halo files [reading] **
    • ICO files [reading]
    • IFF files [reading]
    • JBIG [reading, writing] *
    • JNG files [reading]
    • JPEG/JIF files [reading, writing]
    • KOALA files [reading]
    • LBM files [reading]
    • Kodak PhotoCD files [reading]
    • MNG files [reading]
    • PCX files [reading]
    • PBM files [reading, writing]
    • PGM files [reading, writing]
    • PNG files [reading, writing]
    • PPM files [reading, writing]
    • PhotoShop files [reading]
    • Sun RAS files [reading]
    • TARGA files [reading]
    • TIFF files [reading, writing]
    • WBMP files [reading, writing]

    * only via external plugin
    ** only grayscale


Freeimage loads the various file-formats using different libraries, e.g. libtiff or zlib. When loading a file, the data is converted internally using the structure BITMAPINFO from the Win32-API (even when it is compiled under linux). This approach makes it easy converting from format x to format y. To support a specific file-format there are needed only 2 converters, one for loading from the file to a BITMAPINFO and the other for saving from BITMAPINFO into the file:

FIBITMAP         *FreeImage_Load(FREE_IMAGE_FORMAT fif, 
                                 const char *filename, int flags);
BOOL              FreeImage_Save(FREE_IMAGE_FORMAT fif, FIBITMAP *dib, 
                                 const char *filename, int flags);
FREE_IMAGE_FORMAT FreeImage_GetFIFFromFilename(const char *filename);

If you need more infos, download the source, want to discuss or, even better, want to develop additional features, you can visit the freeimage homepage.

Some programming hints can be found here: FreeImage FAQ.


The code was written using:

freeimage freeimage-homepage
Vassili Bourdo code for the shell His homepage
Chris Maunder for the CDIBSectionLite-class


Version 1.0 25. Sept. 2001
  • Initial publication
Version 2.4.2 7. Oct. 2001
  • uses freeimage 2.4.2 (see changes)
  • wait-Cursor
  • exception-handling while loading
Version 2.5.1 14. March 2002
  • loading in background thread -> gui still usable when operation is long
  • using the ClipBox: transforming only the neccessary pixels from the buffer to the screen -> improved drawing-speed (much faster)
  • uses freeimage 2.5.1 (see changes)
Version 2.5.2 26. March 2002
  • uses freeimage 2.5.2 beta 1 (see changes)
  • mouseWheel = Zoom in/out
  • Progress shown in Statusbar while working in background-thread
  • multipage-support
Version 2.5.3 22. April 2002
  • uses freeimage 2.5.3 beta 1 (see changes)
  • fixed some problems loading gray-images and 16-bit tiff
  • sample showing opening an image from a resource (click on res)
  • integrated code for clipboard copy/paste
  • improved drawing:
    if image is not scaled: SetDIBitsToDevice(), otherwise StretchDIBits()
  • some minor fixes

Coming versions (maybe)

  • edit ontop of images
  • activeX Control
  • console für batch ?
  • I plan to continue the Graphics-Suite to display vector-data on top of the images. The vector-information is stored in SVG-files. For more info about SVG see w3c and abobe.


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

Markus Loibl
Germany Germany
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionHow can I save the changed image data into struct FIBITMAP Pin
18-May-02 9:24
suss18-May-02 9:24 
AnswerRe: How can I save the changed image data into struct FIBITMAP Pin
mloibl20-May-02 21:40
membermloibl20-May-02 21:40 
GeneralRe: How can I save the changed image data into struct FIBITMAP Pin
22-May-02 7:37
suss22-May-02 7:37 
Generalanother bug (changing color) + workaround Pin
mloibl1-May-02 23:12
membermloibl1-May-02 23:12 
GeneralPCD Loading Pin
Saul Ollervides26-Apr-02 5:48
memberSaul Ollervides26-Apr-02 5:48 
GeneralRe: PCD Loading Pin
mloibl2-May-02 8:13
membermloibl2-May-02 8:13 
GeneralRe: PCD Loading Pin
13-Jun-02 22:08
suss13-Jun-02 22:08 
QuestionHow can I load a bitmap from resource ..... Pin
15-Apr-02 7:56
suss15-Apr-02 7:56 
and display it inside a client dc ??

I load the bitmap to a HGLOBAL, as an example said
on the freeimage web site "convert a HBitmap to a FreeImage", in print preview everything is correct, but as a printer output I get only a black rectangle, where the image should be placed, don't know why ?? Any help ???

How do i convert a HBITMAP to a FreeImage image?

GetObject(hbmScreen, sizeof(BITMAP), (LPSTR)&bmp);
int cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel);

FIBITMAP *dib = FreeImage_Allocate(bmp.bmWidth, bmp.bmHeight, cClrBits );

GetDIBits(hdcScreen, hbmScreen, 0, bmp.bmHeight, FreeImage_GetBits(dib),
FreeImage_GetInfo(dib), DIB_RGB_COLORS);

AnswerRe: How can I load a bitmap from resource ..... Pin
mloibl1-May-02 22:52
membermloibl1-May-02 22:52 
GeneralRe: How can I load a bitmap from resource ..... Pin
2-May-02 4:42
suss2-May-02 4:42 
GeneralRe: How can I load a bitmap from resource ..... Pin
mloibl2-May-02 7:59
membermloibl2-May-02 7:59 
GeneralRe: How can I load a bitmap from resource ..... Pin
3-May-02 0:07
suss3-May-02 0:07 
GeneralRe: How can I load a bitmap from resource ..... Pin
mloibl3-May-02 0:17
membermloibl3-May-02 0:17 
General16-bit grayscale Pin
5-Apr-02 9:07
suss5-Apr-02 9:07 
GeneralRe: 16-bit grayscale Pin
mloibl7-Apr-02 1:32
membermloibl7-Apr-02 1:32 
GeneralRe: 16-bit grayscale Pin
mloibl1-May-02 22:51
membermloibl1-May-02 22:51 
GeneralI couldn't save as the bitmap .......... Pin
31-Mar-02 19:30
suss31-Mar-02 19:30 
GeneralRe: I couldn't save as the bitmap .......... Pin
3-Apr-02 21:54
suss3-Apr-02 21:54 
General16-bit not supported (?) Pin
29-Mar-02 15:39
suss29-Mar-02 15:39 
GeneralRe: 16-bit not supported (?) Pin
mloibl1-May-02 22:50
membermloibl1-May-02 22:50 
GeneralCode for Copy image to clipboard Pin
mloibl27-Mar-02 1:04
membermloibl27-Mar-02 1:04 
QuestionHow to read an image into opengl texture ? Pin
26-Mar-02 20:36
suss26-Mar-02 20:36 
AnswerRe: How to read an image into opengl texture ? Pin
mloibl26-Mar-02 21:04
membermloibl26-Mar-02 21:04 
AnswerRe: How to read an image into opengl texture ? Pin
mloibl27-Mar-02 5:53
membermloibl27-Mar-02 5:53 
GeneralFIBITMAP 2 HBITMAP........ Pin
25-Mar-02 21:47
suss25-Mar-02 21:47 

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 | Cookies | Terms of Use | Mobile
Web01 | 2.8.190419.4 | Last Updated 2 May 2002
Article Copyright 2001 by Markus Loibl
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid