如何将png资源加载到对话框上的图片控件中?
我在 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
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
.将 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::加载.(注意: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
.
相关文章