MFC - 是否有任何方法将png资源加载到对话框中的图片控件? [英] MFC - Is there any way to load a png resource into picture control on a dialog box?
问题描述
我在OnInitDialog()上尝试了以下代码,但没有显示。
I tried the following code on OnInitDialog() but nothing was shown.
m_staticLogo.SetBitmap(::LoadBitmap(NULL, MAKEINTRESOURCE(IDB_LOGO)));
其中m_staticLogo是静态图片控件,IDB_LOGO是png文件的资源ID。 >
where m_staticLogo is the static picture control and IDB_LOGO is the resource ID of the png file.
推荐答案
正如你所发现的, :: LoadBitmap
c $ c> :: LoadImage )只处理 .bmp
s。到目前为止,最简单的解决方案是将图像转换为 .bmp
。
As you’ve discovered, ::LoadBitmap
(and the newer ::LoadImage
) only deal with .bmp
s. By far the easiest solution is to convert your image to a .bmp
.
如果图像具有透明度可以转换为32位ARGB位图(这里是一个名为 AlphaConv 的工具,可以转换它)。然后使用 CImage
类 LoadFromResource
方法加载图像。将 CImage
传递给 m_staticLogo.SetBitmap()
。
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). Then load the image using the CImage
class LoadFromResource
method. Pass the CImage
to m_staticLogo.SetBitmap()
.
但是,如果你真的需要它是 .png
,就可以做到。
But if you really need it to be a .png
, it can be done.
方法1(更简单的方法):使用 CImage :: Load 从文件加载
.png
/ code>。将 CImage
传递给 m_staticLogo.SetBitmap()
。
Method 1 (the easier way): Load the .png
from a file using CImage::Load
. Pass the CImage
to m_staticLogo.SetBitmap()
.
方法2(更难的方式):通过将资源加载到COM IStream中,从资源加载
并使用 .png
CImage :: Load
。 (注意: CImage :: LoadFromResource
看起来很诱人,但不能使用 .png
图形)。要将资源导入COM IStream
,请参见此Codeproject article 。注意这篇文章适用于 Gdiplus :: Bitmap
,但关键部分是如何创建 IStream
,你应该能够适应 CImage
。最后,将 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
. (NOTE: CImage::LoadFromResource
looks tempting but will not work with a .png
graphic). To get the resource into a COM IStream
, see this Codeproject article. 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
. Finally, pass the CImage
to m_staticLogo.SetBitmap()
.
修改:更新为使用
CImage
,比 Gdiplus :: Bitmap
。
这篇关于MFC - 是否有任何方法将png资源加载到对话框中的图片控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!