Click here to Skip to main content
15,121,678 members
Articles / Desktop Programming / MFC
Posted 11 Mar 2002


72 bookmarked

Load JPEG and (transparant) GIF picture files from a resource in 3 lines code.

Rate me:
Please Sign up or sign in to vote.
4.90/5 (26 votes)
16 Jul 20023 min read
An article on simple loading GIF, JPEG pictures from a resource (.RC) (no MFC)

Sample Image - gifload.gif

If you are a beginner you may just want to scroll down to the end and just use the module ;)

Well where should I start? Yesterday, I tried to find a source for getting my JPG and GIF files in a Window. And I only found a "Few" (like 10) examples on that. after 6 hours surfing on the internet. 

The bad thing only with the sources were that:

  • Most of the time they used MFC (real MFC so it needs the DLL's), I don't like MFC (but that is possible because I'm from the ASM world).
  • The sources needed almost always a very BIG library, and sometimes they want money for it too.
  • They where coded in C++ (don't care of course but you don't have to use it).
  • The sources were very complicated, why should we reinvent the wheel?
  • I wanted to make my app so small as possible, and all this libs were making mine bigger and bigger.

Alright, well I found 1 source that was useful. I rewrote all the routines and removed the original calls to the header. They were too complicated too. I made "only" 3 calls.

  1. To add the picture to the window (only have to give the resource Id and the position).
  2. A function that will repaint it. (when u minimize the program).
  3. And a final function to free the pictures from memory.

is that simple or what? :)

ok let's go to some code, I wrote a linked list to store all the pictures in memory. I was thinking of making a class of all this, but well I know C a lot better then C++ so that is why I didn't make one. (I could do it, but it would take a lot more time). 

Loading from the resource:

HRSRC res = FindResource(GetModuleHandle(NULL),
if (res) 
    struct Pictures *pPicture = (struct Pictures*)
                                          malloc (sizeof(struct Pictures));
    HGLOBAL mem = LoadResource(GetModuleHandle(NULL), res);
    void *data = LockResource(mem);
    size_t sz = SizeofResource(GetModuleHandle(NULL), res);
    pPicture->Picture = LoadPicture(hWnd, (u_char*)data, sz, 
                &pPicture->PictureWitdh, &pPicture->PictureHeight);

you see the last line that calls another procedure I wrote:

IPicture *LoadPicture(HWND hWnd, const unsigned char *data, 
                           size_t len,long *ret_w, long *ret_h, 
                           OLE_XSIZE_HIMETRIC *cx, OLE_YSIZE_HIMETRIC *cy)
  HDC dcPictureLoad;
  IPicture *pic = NULL;

  HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, len);
  LPVOID pvData = GlobalLock( hGlobal );

  LPSTREAM pStream = NULL;
  HRESULT hr = CreateStreamOnHGlobal( hGlobal, TRUE, &pStream );
  OleLoadPicture(pStream, 0, FALSE,IID_IPicture, (void **)&pic);


  dcPictureLoad = GetDC(hWnd);
  *ret_w = MAP_LOGHIM_TO_PIX(*cx, GetDeviceCaps(dcPictureLoad, LOGPIXELSX));
  *ret_h = MAP_LOGHIM_TO_PIX(*cy, GetDeviceCaps(dcPictureLoad, LOGPIXELSX));

  return pic;

This will make from a *char from a IStream and locates the picture from it using:

OleLoadPicture(pStream, 0, FALSE,IID_IPicture, (void **)&pic);

You are probably thinking "no way, that is a MFC procedure, while you made a statement you didn't like it". True, but this procedure will not force you to distribute the MFC DLL with the program. It is just working fine (and it even don't link to the MFC42.dll). Well anyway beside that this procedure is very nice to use, I mean why should we write a decrytor / decompressor for image formats while they already exists in windows. (this method is also used in Internet Explorer) so I guess everybody got the DLL that is needed for this. The best thing is that this method will also support Transparant GIFs. The project includes a transparent GIF and works out fine.

Here we will calculate the RECT size and coordinates.

RECT bounds; = pPicture->PositionY;
bounds.bottom = pPicture->PositionY + pPicture->PictureHeight;
bounds.left = pPicture->PositionX;
bounds.right = pPicture->PositionX + pPicture->PictureWitdh;

The last thing you need to know is the display algorithm. (well if you want of course).

                          bounds.left, bounds.bottom, 
                          bounds.right - bounds.left,
                 - bounds.bottom, 
                          0, 0, 
                          pPicture->cx, pPicture->cy, NULL);
there you go.

Now comes the best part, You do not have to know all this code because I made 3 simple routines, explained below.

void AddPicture(HWND hWnd,int ResourceHandle,int PositionX,int PositionY);
// Procedure: AddPicture
// HWND hWnd          = The handle of the window where you want to add a picture
// int ResourceHandle = The RecourceId (u can use the ALIAS you gave in .RC file) 
//                      example: ID_BLAH
// int PositionX      = The X coordinate on the window where the picture 
//                      should popup :)
// int PositionY      = The Y coordinate .....bleh

void RepaintPictures(HWND hWnd);
// Procedure: RepaintPictures
// HWND hWnd          = The window handle that needs to be repainted. 
//                      example: when it gets Msg WM_PAINT

void RemovePictures(HWND hWnd);
// Procedure: RepaintPictures
// HWND hWnd          = The window handle frees all the pictures (on
//                      that window) example: when it gets Msg WM_CLOSE
I guess all the people are laughing now why so much companies want to make money with libraries that can do the same (or maybe a little more), maybe not?... well I am though :) Thanks for reading this and I hope this is a little clear to you. if not then just download the whole project and see the example - it is really easy, I promise.


17 July 2002 - updated to allow multiple images to be displayed


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

Netherlands Netherlands
When you understand they don't own you, you own them.

Comments and Discussions

GeneralMy vote of 5 Pin
Manoj Kumar Choubey26-Feb-12 21:57
professionalManoj Kumar Choubey26-Feb-12 21:57 
GeneralLoad from file Pin
wmarcuci2-Mar-10 9:16
Memberwmarcuci2-Mar-10 9:16 
Generalload image Pin
roni_hazan25-Oct-07 12:14
Memberroni_hazan25-Oct-07 12:14 
QuestionWilling to pay for a function Pin
Ali Imran Khan Shirani12-Jan-07 14:54
MemberAli Imran Khan Shirani12-Jan-07 14:54 
AnswerRe: Willing to pay for a function Pin
Christian Graus12-Jan-07 15:32
protectorChristian Graus12-Jan-07 15:32 
Questionworking in a regular MFC app? Pin
vjedlicka27-Apr-06 12:20
Membervjedlicka27-Apr-06 12:20 
GeneralRotating an Image Pin
Alex Evans4-Jan-05 10:27
MemberAlex Evans4-Jan-05 10:27 
GeneralImages look crappy Pin
Anonymous13-Sep-04 17:44
MemberAnonymous13-Sep-04 17:44 
GeneralShowing a GIF in the status bar Pin
Alex Evans26-Jun-04 14:18
MemberAlex Evans26-Jun-04 14:18 
GeneralRe: Showing a GIF in the status bar Pin
lekshmimnest18-Jan-07 22:47
Memberlekshmimnest18-Jan-07 22:47 
GeneralIID_IPicture, link error. Pin
vinquick8-May-04 16:01
Membervinquick8-May-04 16:01 
GeneralRe: IID_IPicture, link error. Pin
Avinesh B26-May-04 15:08
MemberAvinesh B26-May-04 15:08 
GeneralRe: IID_IPicture, link error. Pin
Anonymous13-Jul-04 7:53
MemberAnonymous13-Jul-04 7:53 
GeneralRe: IID_IPicture, link error. Pin
squezel11-Oct-04 11:59
susssquezel11-Oct-04 11:59 
QuestionHow do I display GIF file in a windows dialog (or a toolbar button)? Pin
guoalan30-Apr-04 10:41
Memberguoalan30-Apr-04 10:41 

I am new to the GUI design and need help with the following:

I am using Visual Studio 7.0, and have a standard windows
dialog created with the VS tools. I need to display a GIF picture
on the dialog.

Currently I am using the following code to load a BMP file into
the static control (hwndSystemTestImage):



lRet = SendMessage(hwndSystemTestImage,(UINT) STM_SETIMAGE,IMAGE_BITMAP,(LPARAM) hPic);

However, the problem is that this only works for BMP images. I need to be able to show GIF and animated GIFs on my dialog.

Is there any way i can display GIFs?


QuestionHow to load gif picture in VC++? Pin
Member 91404228-Feb-04 11:10
MemberMember 91404228-Feb-04 11:10 
Generalgoofy, you just reinvented the wheel Pin
Emcee Lam11-Dec-02 23:10
MemberEmcee Lam11-Dec-02 23:10 
GeneralRe: goofy, you just reinvented the wheel Pin
Bengi9-Feb-03 22:06
MemberBengi9-Feb-03 22:06 
GeneralRe: goofy, you just reinvented the wheel Pin
Emcee Lam10-Feb-03 6:59
MemberEmcee Lam10-Feb-03 6:59 
GeneralRe: goofy, you just reinvented the wheel Pin
Hesham Amin28-Jan-04 8:25
MemberHesham Amin28-Jan-04 8:25 
GeneralRe: goofy, you just reinvented the wheel Pin
Doesn't like MFC26-Aug-05 18:17
sussDoesn't like MFC26-Aug-05 18:17 
QuestionHow to Load image from a server?? Pin
yixin11-Dec-02 21:47
Memberyixin11-Dec-02 21:47 
Generalhey goofy, ur code is sooooo bad :-) Pin
Bengi5-Dec-02 20:57
MemberBengi5-Dec-02 20:57 
GeneralSaveAsFile Function Pin
Enrique Juarez21-Nov-02 17:36
MemberEnrique Juarez21-Nov-02 17:36 
GeneralRe: SaveAsFile Function Pin
ahyqs15-Jul-06 23:10
Memberahyqs15-Jul-06 23:10 

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.