How to load a png resource into picture control on a dialog box?(如何将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)只处理与 .bmps.到目前为止,最简单的解决方案是将您的图像转换为 .bmp.
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.
如果图片有透明度,可以转换成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.
这篇关于如何将png资源加载到对话框上的图片控件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将png资源加载到对话框上的图片控件中?
基础教程推荐
- 这个宏可以转换成函数吗? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
