Click here to Skip to main content
15,884,938 members
Articles / Desktop Programming / MFC
Article

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 301.3K   6.9K   73   56
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),
                         MAKEINTRESOURCE(ResourceHandle),"BINARY");
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 );
  memcpy(pvData,data,len);
  GlobalUnlock(hGlobal);

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

  pic->get_Width(cx);
  pic->get_Height(cy);

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

  ReleaseDC(hWnd,dcPictureLoad);
  return pic;
}pre>

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;
bounds.top = 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).

pPicture->Picture->Render(dcRepaintPictures, 
                          bounds.left, bounds.bottom, 
                          bounds.right - bounds.left,
                          bounds.top - 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.

History

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

License

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


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

Comments and Discussions

 
GeneralOLELoadPicture Pin
Paul M Watt13-Nov-02 9:37
mentorPaul M Watt13-Nov-02 9:37 
QuestionHow load picture from file? Pin
12-Jul-02 0:15
suss12-Jul-02 0:15 
AnswerRe: How load picture from file? Pin
[goofy]13-Jul-02 21:13
[goofy]13-Jul-02 21:13 
GeneralRe: How load picture from file? Pin
Moll22-Jul-02 3:03
Moll22-Jul-02 3:03 
GeneralRe: How load picture from file? Pin
Sandford6-Aug-02 5:43
Sandford6-Aug-02 5:43 
GeneralUpdate has been done Pin
[goofy]5-Jul-02 23:37
[goofy]5-Jul-02 23:37 
Generalbugs with this Pin
Mister Transistor30-May-02 5:25
Mister Transistor30-May-02 5:25 
Generalbugs fixed Pin
Mister Transistor4-Jul-02 3:57
Mister Transistor4-Jul-02 3:57 
QuestionHow to show it on the MDI client's background ? Pin
Eric Lee30-May-02 2:45
Eric Lee30-May-02 2:45 
GeneralI got a compile problem Pin
neoosk21-May-02 15:53
neoosk21-May-02 15:53 
QuestionPossible bugs in code? Pin
Eugene Polonsky12-Mar-02 11:50
Eugene Polonsky12-Mar-02 11:50 
AnswerRe: Possible bugs in code? Pin
Dave Loeser23-May-02 8:47
Dave Loeser23-May-02 8:47 
GeneralJPEG and transparency Pin
Jose Manuel Muélledes12-Mar-02 8:13
Jose Manuel Muélledes12-Mar-02 8:13 
GeneralRe: JPEG and transparency Pin
[goofy]12-Mar-02 8:20
[goofy]12-Mar-02 8:20 
GeneralRe: JPEG and transparency Pin
Matthias Mann18-Jul-02 4:10
Matthias Mann18-Jul-02 4:10 
Generalwhy people want money Pin
Chris Losinger12-Mar-02 3:43
professionalChris Losinger12-Mar-02 3:43 
GeneralRe: why people want money Pin
[goofy]12-Mar-02 3:44
[goofy]12-Mar-02 3:44 
GeneralRe: why people want money Pin
Chris Losinger12-Mar-02 4:02
professionalChris Losinger12-Mar-02 4:02 
GeneralRe: why people want money Pin
[goofy]12-Mar-02 4:17
[goofy]12-Mar-02 4:17 
GeneralRe: why people want money Pin
Chris Losinger12-Mar-02 4:21
professionalChris Losinger12-Mar-02 4:21 
GeneralRe: why people want money Pin
SGarratt12-Mar-02 5:00
SGarratt12-Mar-02 5:00 
GeneralRe: why people want money Pin
17-Mar-02 11:47
suss17-Mar-02 11:47 
Generaloh, nice advertisement Pin
17-Mar-02 12:40
suss17-Mar-02 12:40 
GeneralRe: why people want money Pin
13-May-02 7:59
suss13-May-02 7:59 

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.