Click here to Skip to main content
Licence 
First Posted 11 Mar 2002
Views 223,935
Bookmarked 66 times

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

By | 16 Jul 2002 | Article
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

About the Author

[goofy]



Netherlands Netherlands

Member

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmembermanoj kumar choubey20:57 26 Feb '12  
GeneralLoad from file Pinmemberwmarcuci8:16 2 Mar '10  
Generalload image Pinmemberroni_hazan11:14 25 Oct '07  
QuestionWilling to pay for a function PinmemberRAD CPP13:54 12 Jan '07  
AnswerRe: Willing to pay for a function PinstaffChristian Graus14:32 12 Jan '07  
Questionworking in a regular MFC app? Pinmembervjedlicka11:20 27 Apr '06  
GeneralRotating an Image PinmemberAlex Evans9:27 4 Jan '05  
GeneralImages look crappy PinsussAnonymous16:44 13 Sep '04  
GeneralShowing a GIF in the status bar PinmemberAlex Evans13:18 26 Jun '04  
GeneralRe: Showing a GIF in the status bar Pinmemberlekshmimnest21:47 18 Jan '07  
GeneralIID_IPicture, link error. Pinmembervinquick15:01 8 May '04  
GeneralRe: IID_IPicture, link error. PinmemberAvinesh B14:08 26 May '04  
GeneralRe: IID_IPicture, link error. PinsussAnonymous6:53 13 Jul '04  
GeneralRe: IID_IPicture, link error. Pinsusssquezel10:59 11 Oct '04  
QuestionHow do I display GIF file in a windows dialog (or a toolbar button)? PinsussSam Morgan9:41 30 Apr '04  
QuestionHow to load gif picture in VC++? Pinmemberaamirraza10:10 28 Feb '04  
Generalgoofy, you just reinvented the wheel PinmemberEmcee Lam22:10 11 Dec '02  
GeneralRe: goofy, you just reinvented the wheel PinmemberBengi21:06 9 Feb '03  
GeneralRe: goofy, you just reinvented the wheel PinmemberEmcee Lam5:59 10 Feb '03  
GeneralRe: goofy, you just reinvented the wheel Pinmemberhspc7:25 28 Jan '04  
GeneralRe: goofy, you just reinvented the wheel PinsussDoesn't like MFC17:17 26 Aug '05  
QuestionHow to Load image from a server?? Pinmemberyixin20:47 11 Dec '02  
Generalhey goofy, ur code is sooooo bad :-) PinmemberBengi19:57 5 Dec '02  
GeneralSaveAsFile Function PinmemberEnrique Juarez16:36 21 Nov '02  
GeneralRe: SaveAsFile Function Pinmemberahyqs22:10 15 Jul '06  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 17 Jul 2002
Article Copyright 2002 by [goofy]
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid