I'm having trouble to load a bitmap from resource. I have a project that I want to maintain. The images are being loaded from files, but I want to load them from resource.
So, the code below is working:
WCHAR path[MAX_PATH] = TEXT("C:\\nananana...");
pBitmapClose = Bitmap::FromFile(path);
But, when I try to use from resource, it is not working. I have tried several parameters as bellow:
pBitmapClose = Bitmap::FromResource(g_hInstance, MAKEINTRESOURCE(IDB_BTN_CLOSE));
or
pBitmapClose = Bitmap::FromResource((HINSTANCE) GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BTN_CLOSE));
Can anyone PLEASE help me?
What format are your resources in?
GDI+ can only load bitmap (.BMP) images from resources. If you want to load PNG or JPG images from a resource you need to kludge it using a stream. See http://www.codeproject.com/Articles/3537/Loading-JPG-PNG-resources-using-GDI for a handy class that can do it.
BOOL CreateBitmap(LPCTSTR szFileName)
{
if(::PathFileExists(szFileName))
m_pBitmap = Bitmap::FromFile(T2CW(szFileName));
else
{
m_pBitmap = Bitmap::FromResource( ModuleHelper::GetResourceInstance(), MAKEINTRESOURCE(IDB_NOFIND));
}
return !IsNull();
}
// the code will work
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.