I have a Bitmap image that i want to load dynamically. But I am unable to load it.
it does not seem to be working.
Can someone please help me.
You can also try something like this:
CImage image; image.Load(_T("C:\\image.png")); CBitmap bitmap; bitmap.Attach(image.Detach());
According to CBitmap documentation:
LoadBitmap() function takes resource identifier of the bitmap or resource id of the bitmap.
You can't specify the path of the bitmap file.
MyProject.rc ------------ MYBMP BITMAP "res\myimage.bmp"
and make sure that resource.h does not have any entry of MYBMP otherwise during preprocessing its replaced by id and ultimately
LoadBitmap() will fail since application can't locate the resource as
Now do this :
CBitmap bmp; bmp.LoadBitmap(L"MYBMP");
It will definitely load the bitmap.
To load a bitmap from a file, you want to use
LoadImage with the
CBitmap doesn't support directly reading from a .bmp file. You can instead make use of
CImage class as suggested in other answers. You'll need to include
atlimage.h in your code to make
#include <atlimage.h> : CImage img; img.Load (_T("C:\\image.bmp")); CBitmap bitmap; bitmap.Attach(img.Detach());
Another way is to load the image using
LoadImage Win32 API and then attaching
CBitmap to that:
HBITMAP hBitmap = (HBITMAP)LoadImage(NULL,"c:\\image.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); if (hBitmap != NULL) bitmap.Attach(hBitmap);
CImage doesn't work with png last time I tried / checked. Have a look at CxImage - http://www.codeproject.com/KB/graphics/cximage.aspx .
It could be as simple as you forgetting to escape the backslash. Instead of
Otherwise you're passing an invalid path to the LoadBitmap method.