将 PNG 资源加载到 CBitmap 中
如何将 png 资源加载到 CBitMap
中?当我尝试这样做时,它似乎不起作用:
CImage image;
image.LoadFromResource(AfxGetInstanceHandle(), IDB_PNG1);
bitmap.Attach(image.Detach());
它给了我一个错误资源类型未找到。还有其他方法可以加载 PNG 资源吗?
How do I load a png resource into a CBitMap
? When I try this it doesn't seem to work:
CImage image;
image.LoadFromResource(AfxGetInstanceHandle(), IDB_PNG1);
bitmap.Attach(image.Detach());
It gives me an error resource type not found. Is there any other way to load a PNG resource?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用的是 VS2008 或更新版本(MFC 功能包),则可以使用从
CBitmap
派生并包含在
中的CPngImage
>。CPngImage
是一个如果您想使用
CPngImage
,您需要使用资源类型"PNG"
:用法示例
If you are using VS2008 or newer (MFC Feature Pack) you can use
CPngImage
which derives fromCBitmap
and is contained in<afxtoolbarimages.h>
.CPngImage
is an internal class:If you want to use
CPngImage
you need to use the resource type"PNG"
:Example usage
LoadFromResource
在内部使用LoadImage
,我上次尝试时它不支持 PNG,尽管文档中有说明。您可以与CxImage
链接(查看 CodeProject),但这需要大量工作,但几乎没有真正的好处。我最终所做的就是忘记 PNG,使用
AlphaConv
将 PNG 转换为 32 位 Windows 位图,并使用CImage
来处理它们。另一个优点是您可以在 MFC 中将它们与CBitmapButton
等一起使用,而无需任何转换。另一种破解方法是将资源内容提取到临时文件中,然后使用 CImage::Load 从那里读取它。如果我发现你在我的一个代码库中这样做,我会打你的脸;)
LoadFromResource
usesLoadImage
internally, it doesn't support PNG last time I tried, despite what the documentation says. You can link withCxImage
(look on CodeProject) but it's a lot of work for little real benefit.What I did in the end was forget about PNG, use
AlphaConv
to convert PNG's into 32-bit Windows bitmaps and useCImage
with those. Additional advantage is that you can use them withCBitmapButton
etc. in MFC without any transformations.Another way to hack around it is to extract the resource contents to a temporary file, then use
CImage::Load
to read it from there. If I'd catch you doing that in one of my code bases I'd punch you in the face though ;)如果您确实不需要
CBitmap
,而只需要一个句柄,那么这可能对您有用。或者,如果您可以从
HBITMAP
获取CBitmap
,则此函数返回也很好。我在商业应用程序上使用它并且它有效。
If you do not really need a
CBitmap
, but only an handle this may useful for you.Or if you can get a
CBitmap
from theHBITMAP
this function returns it is also good.I use this on a commercial application and it works.
使用像
CxImage
这样的库 (http://www.xdp.it/cximage .htm)为您完成所有繁重的工作。代码变得像这样简单:-(请注意,我是从内存中编写此代码的 - 您可能需要稍微调整它才能使其工作!)
我经常使用
CxImage
来绘制图形在我的程序中,它极大地简化了事情。Use a library like
CxImage
(http://www.xdp.it/cximage.htm) to do all the heavy lifting for you. The code becomes as simple as :-(note that I'm writing this code from memory -- you may need to tweak it slightly to get it to work!)
I use
CxImage
a lot for the graphics in my programs and it simplifies things tremendously.