Search code examples
visual-studio-2010visual-studiomfcbmp

How to load a bmp file in visual studio 2010


I am pretty new to Visual Studio C++. I am trying to load a bmp file from my system with specific path and show it on picture control in mfc, but m unable to do so. Can any one guide me here. I tried the following but i am getting an error..

Code:

filePath = "images/0000.4d.bmp";
ASSERT(filePath);

HBITMAP hBitmap= NULL;
hBitmap = (HBITMAP) LoadImage (
  0, 
  filePath, 
  IMAGE_BITMAP, 
  0, 0, 
  LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);


IDB_PICTURE2.LoadBitmapW(MAKEINTRESOURCE(hBitmap));
m_picture.SetBitmap(IDB_PICTURE2);

The error occurs when I am trying to load the bmp file. Thank you.

Error:

Detected memory leaks!
Dumping objects ->
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(156) : {525} normal block at 0x0034B848, 54 bytes long.
 Data: <   x            > 84 CF B1 78 12 00 00 00 12 00 00 00 01 00 00 00 
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\wincore.cpp(4500) : {518} client block at 0x0034B708, subtype c0, 56 bytes long.
a CObject object at $0034B708, 56 bytes long
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(156) : {511} normal block at 0x0034B1C8, 42 bytes long.
 Data: <   x            > 84 CF B1 78 0C 00 00 00 0C 00 00 00 01 00 00 00 
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\occmgr.cpp(195) : {510} normal block at 0x0034B170, 24 bytes long.
 Data: <                > E8 03 00 00 00 00 00 00 E9 03 00 00 00 00 00 00 
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\occmgr.cpp(181) : {509} normal block at 0x0034B120, 16 bytes long.
 Data: <                > FF FF FF FF 00 00 00 00 00 00 00 00 00 00 00 00 
c:\documents and settings\raja hadi ashraf\my documents\visual studio 2010\projects\mirrorimage\mirrorimage\mirrorimage.cpp(59) : {505} client block at 0x0034AFB0, subtype c0, 12 bytes long.
a CObject object at $0034AFB0, 12 bytes long
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(156) : {494} normal block at 0x0034CDF8, 22 bytes long.
 Data: <   x            > 84 CF B1 78 02 00 00 00 02 00 00 00 01 00 00 00 
Object dump complete.
The program '[0x62C] mirrorImage.exe: Native' has exited with code 3 (0x3).

Solution

  • First test that your hBitmap that it's not NULL then just call:

    m_picture.SetBitmap( hBitmap );
    

    Other:

    1. IDB_PICTURE2 is just a #define in your resource.h file. At compile time it'll just be replaced with a number, ie it's not a class you can call methods on.
    2. MAKEINTRESOURCE is for converting a resource id into a LPCTSTR for use with methods. It does not turn a hBitmap into a resource.
    3. The 'error' you list is just some debug information. It's not the actual error. If LoadBitmap returns NULL call GetLastError for extra information.