简体   繁体   English

如何将png资源加载到对话框的图片控件中?

[英]How to load a png resource into picture control on a dialog box?

I tried the following code on OnInitDialog() but nothing was shown. 我在OnInitDialog()上尝试了以下代码,但没有显示任何内容。

m_staticLogo.SetBitmap(::LoadBitmap(NULL, MAKEINTRESOURCE(IDB_LOGO)));

where m_staticLogo is the static picture control and IDB_LOGO is the resource ID of the png file. 其中m_staticLogo是静态图片控件,IDB_LOGO是png文件的资源ID。

As you've discovered, ::LoadBitmap (and the newer ::LoadImage ) only deal with .bmp s. 正如您所发现的, ::LoadBitmap (和较新的::LoadImage )仅处理.bmp By far the easiest solution is to convert your image to a .bmp . 到目前为止,最简单的解决方案是将图像转换为.bmp

If the image has transparency, it can be converted into a 32-bit ARGB bitmap (here is a tool called AlphaConv that can convert it). 如果图像具有透明度,则可以将其转换为32位ARGB位图(这是一个名为AlphaConv的工具,可以对其进行转换)。 Then load the image using the CImage class LoadFromResource method. 然后使用CImageLoadFromResource方法加载图像。 Pass the CImage to m_staticLogo.SetBitmap() . CImage传递给m_staticLogo.SetBitmap()

But if you really need it to be a .png , it can be done. 但如果你真的需要它是一个.png ,它就可以完成。

Method 1 (the easier way): Load the .png from a file using CImage::Load . 方法1(更简单的方法):使用CImage::Load从文件加载.png Pass the CImage to m_staticLogo.SetBitmap() . CImage传递给m_staticLogo.SetBitmap()

Method 2 (the harder way): Load the .png from a resource by loading the resource into a COM IStream and using CImage::Load . 方法2(更难的方法):通过将资源加载到COM IStream并使用CImage::Load从资源加载.png (NOTE: CImage::LoadFromResource looks tempting but will not work with a .png graphic). (注意: CImage::LoadFromResource看起来很诱人但不适用于.png图形)。 To get the resource into a COM IStream , see this Codeproject article . 要将资源放入COM IStream ,请参阅此Codeproject文章 Note the article works with Gdiplus::Bitmap but the key part is how to create the IStream , which you should be able to adapt for CImage . 请注意,本文适用于Gdiplus::Bitmap但关键部分是如何创建IStream ,您应该能够适应CImage Finally, pass the CImage to m_staticLogo.SetBitmap() . 最后,将CImage传递给m_staticLogo.SetBitmap()

Edit: Updated to use CImage , which is easier than Gdiplus::Bitmap . 编辑:更新以使用CImage ,这比Gdiplus::Bitmap更容易。

For those, who need quick solution, here is a way to load png file from resources using GDI+ (original answer for standard GDI from here - http://www.codeproject.com/Questions/377803/How-to-load-PNG-images-in-mfc ): 对于那些需要快速解决方案的人来说,这是一种使用GDI +从资源加载png文件的方法(标准GDI的原始答案来自此处 - http://www.codeproject.com/Questions/377803/How-to-load-PNG -images-in-mfc ):

bool GdiPlusUtils::LoadBitmapFromPNG(UINT uResourceID, 
    Bitmap** ppBitmapOut, HINSTANCE hInstance /*= NULL*/)
{
    bool bRet = false;

    if (!hInstance)
        hInstance = AfxGetInstanceHandle();

    HRSRC hResourceHandle = ::FindResource(
        hInstance, MAKEINTRESOURCE(uResourceID), L"PNG");
    if (0 == hResourceHandle)
    {
        return bRet;
    }

    DWORD nImageSize = ::SizeofResource(hInstance, hResourceHandle);
    if (0 == nImageSize)
    {
        return bRet;
    }

    HGLOBAL hResourceInstance = ::LoadResource(hInstance, hResourceHandle);
    if (0 == hResourceInstance)
    {
        return bRet;
    }

    const void* pResourceData = ::LockResource(hResourceInstance);
    if (0 == pResourceData)
    {
        FreeResource(hResourceInstance);
        return bRet;
    }

    HGLOBAL hBuffer = ::GlobalAlloc(GMEM_MOVEABLE, nImageSize);
    if (0 == hBuffer)
    {
        FreeResource(hResourceInstance);
        return bRet;
    }

    void* pBuffer = ::GlobalLock(hBuffer);
    if (0 != pBuffer)
    {
        CopyMemory(pBuffer, pResourceData, nImageSize);
        IStream* pStream = 0;
        if (S_OK == ::CreateStreamOnHGlobal(hBuffer, FALSE, &pStream))
        {
            *ppBitmapOut = new Bitmap(pStream);
            pStream->Release();
            bRet = true;
        }
        ::GlobalUnlock(hBuffer);
    }
    ::GlobalFree(hBuffer);

    UnlockResource(hResourceInstance);
    FreeResource(hResourceInstance);

    return bRet;
}

You can add png file as resource using Add Resource command and in the panel choose Import. 您可以使用“添加资源”命令将png文件添加为资源,然后在面板中选择“导入”。

Bitmap and icon it supports. 它支持的位图和图标。 Not sure about png. png不确定。 Alternately, May be you can try the following. 或者,可以尝试以下方法。

  1. open png in MS Paint or some other viewer. 在MS Paint或其他一些查看器中打开png。
  2. Then copy the image portion from that. 然后从中复制图像部分。
  3. Create a resource in MFC resource. 在MFC资源中创建资源。
  4. Paste the copied image to newly created resource. 将复制的图像粘贴到新创建的资源。
  5. Use new resource id in LoadBitmap. 在LoadBitmap中使用新的资源ID。

If you are converting .png image file to .bmp format, you can end up with image clarity. 如果要将.png图像文件转换为.bmp格式,则最终可以获得图像清晰度。 So, catch WM_PAINT message in dialog box class and use 因此,在对话框类中捕获WM_PAINT消息并使用

Graphics::DrawImage method Graphics :: DrawImage方法

To obtain this method link your project with gdiplus.lib library. 要获取此方法,请将项目与gdiplus.lib库链接。

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

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