Going from a JPG/GIF/BMP File to a HBITMAP File Using Plain API

This code has probably been superseded by MFC or OWL or whatever classes, which can load any image file. However, you will most probably do it with plain API, and converting a JPG file using Intel's library can be very painful.

Here is a simple function that loads a JPG/GIF/BMP file into memory and returns a HBITMAP. The function uses IPicture, an OLE interface. #include <olectl.h> and <ole2.h> are also needed.

// Function LoadAnImage: accepts a file name and returns a HBITMAP.
// On error, it returns 0.
HBITMAP LoadAnImage(char* FileName)
   {
   // Use IPicture stuff to use JPG / GIF files
   IPicture* p;
   IStream* s;
   IPersistStream* ps;
   HGLOBAL hG;
   void* pp;
   FILE* fp;


   // Read file in memory
   fp = fopen(FileName,"rb");
   if (!fp)
   return NULL;

   fseek(fp,0,SEEK_END);
   int fs = ftell(fp);
   fseek(fp,0,SEEK_SET);
   hG = GlobalAlloc(GPTR,fs);
   if (!hG)
      {
      fclose(fp);
      return NULL;
      }
   pp = (void*)hG;
   fread(pp,1,fs,fp);
   fclose(fp);

   // Create an IStream so IPicture can 
   // CreateStreamOnHGlobal(hG,false,&s);
   if (!s)
      {
      GlobalFree(hG);
      return NULL;
      }

   OleLoadPicture(s,0,false,IID_IPicture,(void**)&p);

   if (!p)
      {
      s->Release();
      GlobalFree(hG);
      return NULL;
      }
   s->Release();
   GlobalFree(hG);

   HBITMAP hB = 0;
   p->get_Handle((unsigned int*)&hB);

   // Copy the image. Necessary, because upon p's release,
   // the handle is destroyed.
   HBITMAP hBB = (HBITMAP)CopyImage(hB,IMAGE_BITMAP,0,0,
                                    LR_COPYRETURNORG);

   p->Release();
   return hBB;
   }


Comments

  • How to use

    Posted by CNemo on 04/24/2009 04:04pm

    I got it to work via: #1 remove comment characters CreateStreamOnHGlobal(hG,false,&s); //this line should be executed. #2. m_Ctl_BitMap_Pic.SetBitmap(LoadAnImage("c:\\myjpeg.jpg")); // place pic control on dialog and set to bitmap. ...basically there is an error in the article the line in #1 above is commented out and it SHOULD NOT BE.

    Reply
  • How to use

    Posted by e e;:g:7i# on 03/14/2009 12:48pm

    I still do not understand can talk about how to give me call this? What are the parameters?

    Reply
  • useless

    Posted by carl666 on 01/02/2009 07:27am

    See the KB for OleLoadPicture, a lot better of course

    Reply
  • Can not use

    Posted by crazycn110 on 09/29/2007 01:34pm

    warning C4101: 'ps' : unreferenced local variable warning C4700: local variable 's' used without having been initialized Linking...

    Reply
  • Thanks

    Posted by hiren241183 on 09/19/2007 03:34am

    hi , thank you very much.this artical is very helpful to me.

    Reply
  • Application crashes + 1 more question

    Posted by Ali Imran on 01/12/2007 08:39pm

    This is really great resource, thanks alot. 1. My application crashes when I use this function, even if the jpeg exists and has proper jpeg format and extenstion. 2. Michael, may I use your function in RAD C++ GUI Library ? regards

    Reply
  • cxvbvxbzb

    Posted by janardhanpalla on 07/14/2006 05:20am

    vcbb

    Reply
  • New:Play GIF animate within diagram application

    Posted by cindyonlyone on 01/16/2006 09:35pm

    If you want to play GIF animate within your application,you can try XD++ MFC Library with: http://www.ucancode.net

    Reply
  • Passing a CString Variable

    Posted by dacky on 12/15/2005 03:41am

    How can i pass a CString Variable to this function if it accepts char *(pointer to a character)? What if i will use a CFileDialog's .GetPathName Function? this will return a CString variable

    Reply
  • How to save back In the file ( .jpeg ,.bmp format)

    Posted by anantwakode on 09/28/2005 12:28am

    Hi Thanks!! I need to save BITMAP in to file in .jpeg format. Can you post some sample code/way/article to achieve this.. Anant

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • The proliferation of cloud computing options has begun to change the way storage is thought about, procured, and used. IT managers and departments need to think through how cloud options might fit into and complement their onsite data infrastructures. This white paper explains cloud storage and backup, providing advice about the tools and best practices for its implementation and use. Read this white paper for some useful takeaways about how to take advantage of cloud storage for high availability, backup and …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds