简体   繁体   English

无法从资源加载位图

[英]Not able to load Bitmap from resource

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. GDI +只能从资源加载位图(.BMP)图像。 If you want to load PNG or JPG images from a resource you need to kludge it using a stream. 如果要从资源加载PNG或JPG图像,则需要使用流对其进行混淆。 See http://www.codeproject.com/Articles/3537/Loading-JPG-PNG-resources-using-GDI for a handy class that can do it. 请参阅http://www.codeproject.com/Articles/3537/Loading-JPG-PNG-resources-using-GDI ,以获取方便的类。

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 //代码将起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM