WinapiZone.net




Tutorials > WinAPI > Functions > LoadImageFromResource

LoadImageFromResource

This function can load any kind of image from the resource of an executable or dll using COM interfaces.

HBITMAP LoadImageFromResource(UINT resId, LPTSTR resType) { HBITMAP bitmap = NULL; HRSRC resHandle = FindResource(NULL, MAKEINTRESOURCE(resId), resType); if (resHandle) { HGLOBAL memData = LoadResource(NULL, resHandle); if (memData) { DWORD resSize = SizeofResource(NULL, resHandle); HGLOBAL globalData = GlobalAlloc(0, resSize); LPVOID lockedData = GlobalLock(globalData); memcpy(lockedData, memData, resSize); GlobalUnlock(globalData); LPSTREAM pStream = NULL; CreateStreamOnHGlobal(globalData, FALSE, &pStream); LPPICTURE pPicture = NULL; OleLoadPicture(pStream, resSize, TRUE, IID_IPicture, (LPVOID*)&pPicture); if (pPicture) { HBITMAP handle = NULL; pPicture->get_Handle((unsigned int*)&handle); bitmap = (HBITMAP)CopyImage(handle, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG); pPicture->Release(); } pStream->Release(); GlobalFree(globalData); } } return bitmap; }

Requirements

Headers

  • olectl.h
  • ole2.h

Import libraries

  • libuuid.a / uuid.lib
  • libole32.a / ole32.lib
  • liboleaut32.a / oleaut32.lib

Example

HBITMAP jpegImage = LoadImageFromResource(ID_JPEG_IMAGE, _T("JPEG"));
HBITMAP gifImage = LoadImageFromResource(ID_GIF_IMAGE, _T("GIF"));

//In the resource file (.rc)
ID_JPEG_IMAGE JPEG "image.jpg"
ID_GIF_IMAGE GIF "image.gif"

Comments

Joldig wrote:

I should like to draw description an error in your function.
Unfortunately failed me to load a portable network graphics file.
This also move a description of Microsoft only with the GDI+ class!

This is probably due to the free ZIP algorithm?
Or wrong I something? Sorry my English!
(16.08.2009, 16:59)

Your comment:

Name:
E-mail or homepage:
Captcha:
captcha

Script by Alex



This page was last modified 34 months, 3 weeks, 2 days and 9 hours ago