Click here to Skip to main content
Click here to Skip to main content

Library to display an image (JPG/GIF/BMP) in 300 lines/8K of code

, 1 Dec 2002
Rate this:
Please Sign up or sign in to vote.
A simple yet powerful library to display an image in a window.


Here is a sample of what is possible to do with just plain C. This code will display in a window an image that can be in JPG, GIF, BMP, or Windows metafile format.

Compiled with the lcc-Win32 compiler, the code is just 8K. No other language gives you this kind of efficiency. A similar piece of code in C++ weights more than 170K!

The interface is like this:

  1. You tell it the name of the graphic file
    HANDLE h = OpenGraphic(char *filename);

    That handle is used for closing the image.

  2. When painting your window, you pass it the hwnd and the hDC you get from BeginPaint.
    DisplayGraphic(HWND hwnd,HDC hdc);
  3. When finished, you close it with.
    void CloseGraphic(HANDLE h);

Note that CoInitialize() must be called before using these functions.

I did not add "bells and whistles" to keep the code simple to understand. You may add scrollbars, redimensioning whatever.

This code was developed with the lcc-win32 C compiler. You can download it at no charge from here.

P.S.: This contribution was critized because I did not use Microsoft tools. I know I am biased, but I tried to correct my evil ways. I took the dust out of MSVC 6.0 and OK, here is it, complete with .dsw file in a standard Microsoft Project.

Other critics pointed out to a bug in the transcription, where I added a "*" too much. I fixed that, and I thank the people who pointed me that bug.

It is interesting to note that the code size of the program as compiled with MSVC is 32K, with lcc-win32 is just 8K.

Nobody is perfect...

#include "windows.h"
#include "ocidl.h"
#include "olectl.h"

typedef struct tagImgInfo {
  IPicture *Ipic;
  SIZE sizeInHiMetric;
  SIZE sizeInPix;
  char *Path;

static IMG_INFO ImageInfo;

void *OpenGraphic(char *name)
  IPicture *Ipic = NULL;
  SIZE sizeInHiMetric,sizeInPix;
  const int HIMETRIC_PER_INCH = 2540;
  HDC hDCScreen = GetDC(NULL);
  int nPixelsPerInchX = GetDeviceCaps(hDCScreen, LOGPIXELSX);
  int nPixelsPerInchY = GetDeviceCaps(hDCScreen, LOGPIXELSY);
  unsigned short OlePathName[512];

  hr = OleLoadPicturePath(OlePathName,
        (void *)(&Ipic));
  if (hr)
    return 0;
  if (Ipic) {
    // get width and height of picture
    hr = Ipic->lpVtbl->get_Width(Ipic,&;
    if (!SUCCEEDED(hr))
      goto err;
    if (!SUCCEEDED(hr))
      goto err;

    // convert himetric to pixels = (nPixelsPerInchX * +
              HIMETRIC_PER_INCH / 2) / HIMETRIC_PER_INCH; = (nPixelsPerInchY * +
    ImageInfo.sizeInPix = sizeInPix;
    ImageInfo.sizeInHiMetric = sizeInHiMetric;
    ImageInfo.Ipic = Ipic;
    ImageInfo.Path = name;
    return Ipic;

  return 0;
void DisplayGraphic(HWND hwnd,HDC pDC)
  IPicture *Ipic = ImageInfo.Ipic;
  DWORD dwAttr = 0;
  HBITMAP Bmp,BmpOld;
  RECT rc;
  HPALETTE pPalMemOld;

  if (Ipic != NULL)
    // get palette
    OLE_HANDLE hPal = 0;
    HPALETTE hPalOld=NULL,hPalMemOld=NULL;
    hr = Ipic->lpVtbl->get_hPal(Ipic,&hPal);

    if (!SUCCEEDED(hr))
    if (hPal != 0)
      hPalOld = SelectPalette(pDC,(HPALETTE)hPal,FALSE);

    // Fit the image to the size of the client area. Change this
    // For more sophisticated scaling
    // transparent?
    if (SUCCEEDED(Ipic->lpVtbl->get_Attributes(Ipic,&dwAttr)) ||
      // use an off-screen DC to prevent flickering
      HDC MemDC = CreateCompatibleDC(pDC);
      Bmp = 

      BmpOld = SelectObject(MemDC,Bmp);
      pPalMemOld = NULL;
      if (hPal != 0)
        hPalMemOld = SelectPalette(MemDC,(HPALETTE)hPal, FALSE);
/* Use this to show the left corner
      rc.left = = 0;
      rc.right =;
        rc.bottom =;
      // display picture using IPicture::Render
      hr = Ipic->lpVtbl->Render(Ipic,MemDC,

      BitBlt(pDC,0, 0,, 
        MemDC, 0, 0, SRCCOPY);


      if (pPalMemOld)  SelectPalette(MemDC,pPalMemOld, FALSE);

      // display picture using IPicture::Render

    if (hPalOld != NULL) SelectPalette(pDC,hPalOld, FALSE);
    if (hPal)  DeleteObject((HPALETTE)hPal);

void CloseImage(void *Ipict)
  IPicture *ip = (IPicture *)Ipict;

  if (ip == NULL)
    ip = ImageInfo.Ipic;
  if (ip == NULL)


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

Jacob Navia
Web Developer
France France
I am the author of the lcc-win32 C compiler system.

Comments and Discussions

GeneralMy vote of 5 - demonstrates use of non-Microsoft tools PinmemberEdCaputo20-Jun-14 0:10 
GeneralMy vote of 5 Pinmembermanoj kumar choubey26-Feb-12 21:56 
GeneralMy vote of 5 PinmemberRaghavan145-Jan-12 7:27 
GeneralMy vote of 3 PinmemberMarcus Elliott13-Oct-10 1:45 
GeneralI need help PinmemberGbenbam6-Aug-10 3:43 
GeneralLooks like a little bug Pinmembermouseparty17-Feb-06 2:24 
GeneralRe: Looks like a little bug Pinmembervictoryli14-Apr-08 11:12 
GeneralRelease VS PinsussAnonymous6-Feb-05 18:20 
GeneralRotating the image PinmemberAlex Evans4-Jan-05 10:24 
GeneralBinary size PinmemberMr.Prakash2-Mar-04 5:37 
General28 Kb Pinmemberessersr11-Feb-04 10:31 
GeneralRe: 28 Kb Pinmembervictoryli14-Apr-08 9:25 
GeneralPlease help me ! PinsussAnonymous18-Oct-03 2:39 
QuestionRender ? Pinmemberlibrazone20-Jun-03 17:29 
AnswerRe: Render ? PinsussAnonymous24-Jun-03 22:55 
GeneralRe: Render ? Pinmemberkubicle7-Jan-05 21:21 
GeneralCode Size PinsussAnonymous9-Mar-03 0:39 
GeneralRe: Code Size PinmemberKvasi30-Mar-03 6:37 
GeneralRe: Code Size PinsussAnonymous5-May-03 21:16 
GeneralRe: Code Size PinsussAnonymous19-Mar-05 5:35 
really to hell with VC and .NET architecture,
LCC, and GCC rock, who the hell needs the stupid additional dozzens of code generated by dumb head VC. Every developer tries to have minimal binaries so that they are easily distributable.
You write what you think, but do not care about the effort of the person who is giving you a free source, if it is not useful to you and you cant write two words of appreciation, then goto hell and dont join the discussion thread.
Author has done a great job, and was tryign to differentiate between the minimum possible output of the final executable. Did he force you to download and/or discuss his resources ?Mad | :mad:
Quoting everything comes to your stupid brain, and then saying "I hope you will use this message constructively to improve your programming skills." does not heal up the wounds Mad | :mad: . What do yo uknow abotu the strfucture of VC compiler, tell me streight, I would like to have debate on it 'WITH YOU'.
Now, when directX has also been made embedable with c++ applciations and made available opensource in the form of libraries and headers [so that developers dont have to ship the shared DLLs], you will come to say, this is not workign properly as Microsoft directX Dynamic Linked Libraries.

Few questions for you, many technologies you know, especially compilers ?
2.can you compile pure windows API source on Linux ??????I CAN.
3.Did you ever create a compiler or even an interpreter or IDE ?
I am sure all will be
1. No
2. No
3. No
Then why the heck you are criticizing a person who comes with a very good source? although he forgot to mention the platform compatibility.

btw nice job Mr.Author.
Generalneither simple nor powerful PinsussAnonymous3-Dec-02 14:16 
GeneralRe: neither simple nor powerful PinmemberJacob Navia5-Dec-02 4:11 
GeneralRe: neither simple nor powerful PinmemberRufus V. Smith5-Aug-03 7:45 
General170K? no way PinsussAnonymous2-Dec-02 21:55 
GeneralBad news PinsussAnonymous2-Dec-02 20:09 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150129.1 | Last Updated 2 Dec 2002
Article Copyright 2002 by Jacob Navia
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid