如何在VC中将HICON转换为HBITMAP?
如何在VC++中将HICON转换为HBITMAP?
我知道这是一个常见问题解答,但我在 Google 上找到的所有解决方案都不起作用。我需要的是一个接受参数 HICON 并返回 HBITMAP 的函数。
最好能转换为 32 位位图,即使图标是 24 位、16 位或 8 位。
这是代码,不知道哪里错了:
HBITMAP icon_to_bitmap(HICON Icon_Handle) {
HDC Screen_Handle = GetDC(NULL);
HDC Device_Handle = CreateCompatibleDC(Screen_Handle);
HBITMAP Bitmap_Handle =
CreateCompatibleBitmap(Device_Handle,GetSystemMetrics(SM_CXICON),
GetSystemMetrics(SM_CYICON));
HBITMAP Old_Bitmap = (HBITMAP)SelectObject(Device_Handle,Bitmap_Handle);
DrawIcon(Device_Handle, 0,0, Icon_Handle);
SelectObject(Device_Handle,Old_Bitmap);
DeleteDC(Device_Handle);
ReleaseDC(NULL,Screen_Handle);
return Bitmap_Handle;
}
How to convert HICON to HBITMAP in VC++?
I know this is an FAQ but all the solutions I've found on Google don't work. What I need is a function which takes a parameter HICON and returns HBITMAP.
Greatest if possible to make conversion to 32-bit bitmap even the icon is 24-bit, 16-bit or 8-bit.
This is the code, I don't know where it goes wrong:
HBITMAP icon_to_bitmap(HICON Icon_Handle) {
HDC Screen_Handle = GetDC(NULL);
HDC Device_Handle = CreateCompatibleDC(Screen_Handle);
HBITMAP Bitmap_Handle =
CreateCompatibleBitmap(Device_Handle,GetSystemMetrics(SM_CXICON),
GetSystemMetrics(SM_CYICON));
HBITMAP Old_Bitmap = (HBITMAP)SelectObject(Device_Handle,Bitmap_Handle);
DrawIcon(Device_Handle, 0,0, Icon_Handle);
SelectObject(Device_Handle,Old_Bitmap);
DeleteDC(Device_Handle);
ReleaseDC(NULL,Screen_Handle);
return Bitmap_Handle;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
此代码执行此操作:
这是 *.rc 文件中的代码:
这是 *.h 文件中的代码:
this code do it:
and this is the code in the *.rc file:
and this is the code in the *.h file:
我没有现成的代码可供共享,但我认为这非常简单。您必须创建 HBITMAP,创建设备上下文,将位图选择到 DC 中(这将使位图成为该 DC 的绘图区域)。最后调用 DrawIcon() 函数在该 DC 上绘制图标。之后将位图与 DC 分离并销毁 DC。您的位图现在应该已准备就绪。
查看代码后更新:
我相信问题出在 createCompatibleBitmap 调用中。您要求与内存 DC 兼容的位图,但内存 DC 是从选择的 1 位/像素位图开始的。尝试请求与屏幕 DC 兼容的位图。
更新2:您可能想看看这个问题,因为它看起来与你的问题有关。
I don't have code readily available to share, but I think this is pretty easy. You have to create the HBITMAP, create a device context, select the bitmap into the DC (this will make the bitmap the drawing area for this DC). Finally call the DrawIcon() function to draw your icon on this DC. After that detach the bitmap from the DC and destroy the DC. Your bitmap now should be ready to go.
Update after looking at your code:
I believe the problem is in the createCompatibleBitmap call. You are asking for a bitmap compatible with the memory DC, but memory DCs start with a 1 bit/pixel bitmap selected into them. Try asking for a bitmap compatible with the screen DC instead.
Update 2: you may want to look at this question as it seems related to your problem.
我发现了这个(类似的代码对我有用 - 带或不带 alpha 数据的 32x32 图标):
使用 CopyImage (msdn链接)
BM_32_bit_color 位图可能已设置 Alpha *通道*(每个第 4 个字节)!因此 - 在将掩码位添加到颜色数据之前检查它。
I found this(similar code works for me - 32x32 icons with or without alpha data):
used CopyImage (msdn link)
BM_32_bit_color bitmap may be have Alpha *channel*(each 4th byte) already set! So - check for it before u add mask bit to color data.