Click here to Skip to main content
15,878,852 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.1K   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

 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey26-Feb-12 20:57
professionalManoj Kumar Choubey26-Feb-12 20:57 
GeneralLoad from file Pin
wmarcuci2-Mar-10 8:16
wmarcuci2-Mar-10 8:16 
Generalload image Pin
roni_hazan25-Oct-07 11:14
roni_hazan25-Oct-07 11:14 
QuestionWilling to pay for a function Pin
Ali Imran Khan Shirani12-Jan-07 13:54
Ali Imran Khan Shirani12-Jan-07 13:54 
AnswerRe: Willing to pay for a function Pin
Christian Graus12-Jan-07 14:32
protectorChristian Graus12-Jan-07 14:32 
Questionworking in a regular MFC app? Pin
vjedlicka27-Apr-06 11:20
vjedlicka27-Apr-06 11:20 
GeneralRotating an Image Pin
Alex Evans4-Jan-05 9:27
Alex Evans4-Jan-05 9:27 
GeneralImages look crappy Pin
Anonymous13-Sep-04 16:44
Anonymous13-Sep-04 16:44 
GeneralShowing a GIF in the status bar Pin
Alex Evans26-Jun-04 13:18
Alex Evans26-Jun-04 13:18 
GeneralRe: Showing a GIF in the status bar Pin
lekshmimnest18-Jan-07 21:47
lekshmimnest18-Jan-07 21:47 
GeneralIID_IPicture, link error. Pin
vinquick8-May-04 15:01
vinquick8-May-04 15:01 
GeneralRe: IID_IPicture, link error. Pin
Avinesh B26-May-04 14:08
Avinesh B26-May-04 14:08 
For VC++ 6, ensure you have the following libraries in your 'VC98/Lib' directory: OLEPRO32.LIB and OLE32.LIB.
GeneralRe: IID_IPicture, link error. Pin
Anonymous13-Jul-04 6:53
Anonymous13-Jul-04 6:53 
GeneralRe: IID_IPicture, link error. Pin
squezel11-Oct-04 10:59
susssquezel11-Oct-04 10:59 
QuestionHow do I display GIF file in a windows dialog (or a toolbar button)? Pin
guoalan30-Apr-04 9:41
guoalan30-Apr-04 9:41 
QuestionHow to load gif picture in VC++? Pin
Member 91404228-Feb-04 10:10
Member 91404228-Feb-04 10:10 
Generalgoofy, you just reinvented the wheel Pin
Emcee Lam11-Dec-02 22:10
Emcee Lam11-Dec-02 22:10 
GeneralRe: goofy, you just reinvented the wheel Pin
Bengi9-Feb-03 21:06
Bengi9-Feb-03 21:06 
GeneralRe: goofy, you just reinvented the wheel Pin
Emcee Lam10-Feb-03 5:59
Emcee Lam10-Feb-03 5:59 
GeneralRe: goofy, you just reinvented the wheel Pin
Hesham Amin28-Jan-04 7:25
Hesham Amin28-Jan-04 7:25 
GeneralRe: goofy, you just reinvented the wheel Pin
Doesn't like MFC26-Aug-05 17:17
sussDoesn't like MFC26-Aug-05 17:17 
QuestionHow to Load image from a server?? Pin
yixin11-Dec-02 20:47
yixin11-Dec-02 20:47 
Generalhey goofy, ur code is sooooo bad :-) Pin
Bengi5-Dec-02 19:57
Bengi5-Dec-02 19:57 
GeneralSaveAsFile Function Pin
Enrique Juarez21-Nov-02 16:36
Enrique Juarez21-Nov-02 16:36 
GeneralRe: SaveAsFile Function Pin
ahyqs15-Jul-06 22:10
ahyqs15-Jul-06 22: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.