API OleLoadPicture来加载JPG、GIF格式的图片(注:不支持PNG格式,另外GIF只能加载第一帧,且不支持透明)
#include <Ole2.h> #include <OleCtl.h> HBITMAP LoadImage(TCHAR *szFileName) { FILE *fp = _tfopen(szFileName, _T("rb")); if (fp == NULL) return NULL; fseek(fp, 0, SEEK_END); long l_file_len = ftell(fp); rewind(fp); HGLOBAL hGlobal = ::GlobalAlloc(GPTR, l_file_len); if (hGlobal == NULL) { fclose(fp); return NULL; } fread(hGlobal, 1, l_file_len, fp); fclose(fp); IStream *p_stream = NULL; if (FAILED(::CreateStreamOnHGlobal(hGlobal, FALSE, &p_stream))) { ::GlobalFree(hGlobal); return NULL; } IPicture *p_picture; if (FAILED(::OleLoadPicture(p_stream, 0, FALSE, IID_IPicture, (void **) &p_picture))) { p_stream->Release(); ::GlobalFree(hGlobal); return NULL; } p_stream->Release(); ::GlobalFree(hGlobal); HBITMAP hTemp = NULL; p_picture->get_Handle((OLE_HANDLE *) &hTemp); HBITMAP hBitmap = (HBITMAP) ::CopyImage(hTemp, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG); p_picture->Release(); return hBitmap; }