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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3988484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:13:01  来源:igfitidea点击:

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

c++visual-studiomfcpng

提问by david.healed

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。

回答by Nate

As you've discovered, ::LoadBitmap(and the newer ::LoadImage) only deal with .bmps. By far the easiest solution is to convert your image to a .bmp.

正如您所发现的,::LoadBitmap(和更新的::LoadImage)只处理.bmps。到目前为止,最简单的解决方案是将图像转换为.bmp.

If the image has transparency, it can be converted into a 32-bit ARGB bitmap (here is a tool called AlphaConvthat can convert it). Then load the image using the CImageclass LoadFromResourcemethod. Pass the CImageto m_staticLogo.SetBitmap().

如果图像有透明度,则可以将其转换为32位ARGB位图(这里有一个名为AlphaConv的工具可以对其进行转换)。然后使用CImageLoadFromResource方法加载图像。传递CImagem_staticLogo.SetBitmap().

But if you reallyneed it to be a .png, it can be done.

但如果你真的需要它是一个.png,它可以做到。

Method 1 (the easier way):Load the .pngfrom a file using CImage::Load. Pass the CImageto m_staticLogo.SetBitmap().

方法1(更简单的方法):加载.png使用从文件CImage::Load。传递CImagem_staticLogo.SetBitmap().

Method 2 (the harder way):Load the .pngfrom a resource by loading the resource into a COM IStreamand using CImage::Load. (NOTE: CImage::LoadFromResourcelooks tempting but will not work with a .pnggraphic). To get the resource into a COM IStream, see this Codeproject article. Note the article works with Gdiplus::Bitmapbut the key part is how to create the IStream, which you should be able to adapt for CImage. Finally, pass the CImageto m_staticLogo.SetBitmap().

方法2(较硬的方式):装入.png通过装载资源到COM从资源IStream和使用CImage::Load。(注意:CImage::LoadFromResource看起来很诱人,但不适用于.png图形)。要将资源放入 COM IStream,请参阅这篇 Codeproject 文章。请注意本文适用,Gdiplus::Bitmap但关键部分是如何创建IStream,您应该能够适应CImage。最后,将 传递CImagem_staticLogo.SetBitmap()

Edit:Updated to use CImage, which is easier than Gdiplus::Bitmap.

编辑:更新为使用CImage,这比Gdiplus::Bitmap.

回答by Michal Pokluda

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 文件添加为资源,然后在面板中选择导入。

回答by bjskishore123

Bitmap and icon it supports. Not sure about png. Alternately, May be you can try the following.

它支持的位图和图标。不确定png。或者,也许您可​​以尝试以下操作。

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

回答by Raghuram Reddy N

If you are converting .png image file to .bmp format, you can end up with image clarity. So, catch WM_PAINT message in dialog box class and use

如果您将 .png 图像文件转换为 .bmp 格式,您最终可以获得图像清晰度。因此,在对话框类中捕获 WM_PAINT 消息并使用

Graphics::DrawImage method

图形::DrawImage 方法

To obtain this method link your project with gdiplus.lib library.

要获得此方法,请将您的项目与 gdiplus.lib 库链接起来。