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)
returnNULL;
fseek(fp,0,SEEK_END);
int fs = ftell(fp);
fseek(fp,0,SEEK_SET);
hG = GlobalAlloc(GPTR,fs);
if (!hG)
{
fclose(fp);
returnNULL;
}
pp = (void*)hG;
fread(pp,1,fs,fp);
fclose(fp);
// Create an IStream so IPicture can// CreateStreamOnHGlobal(hG,false,&s);if (!s)
{
GlobalFree(hG);
returnNULL;
}
OleLoadPicture(s,0,false,IID_IPicture,(void**)&p);
if (!p)
{
s->Release();
GlobalFree(hG);
returnNULL;
}
s->Release();
GlobalFree(hG);
HBITMAP hB = 0;
p->get_Handle((unsignedint*)&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;
}
Tools:
Add www.codeguru.com to your favorites Add www.codeguru.com to your browser search box IE 7 | Firefox 2.0 | Firefox 1.5.xReceive news via our XML/RSS feed
RATE THIS ARTICLE:
Excellent Very Good Average Below Average Poor
(You must be signed in to rank an article. Not a member? Click here to register)