如何将png资源加载到对话框的图片控件中? [英] How 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
(以及较新的::LoadImage
)仅处理.bmp
.到目前为止,最简单的解决方案是将图像转换为.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位图(这是一个名为
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
.将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
中并使用CImage::Load
来从资源加载.png
. (注意:CImage::LoadFromResource
看起来很吸引人,但不适用于.png
图形).要将资源放入COM IStream
中,请参见此Codeproject文章 .请注意,本文适用于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
容易.
Updated to use CImage
, which is easier than Gdiplus::Bitmap
.
这篇关于如何将png资源加载到对话框的图片控件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!