如何使用GDI绘制ARGB位图?
我有 ARGB
类型的有效 HBITMAP
句柄。 如何使用GDI+绘制它?
我尝试过方法:
graphics.DrawImage(Bitmap::FromHBITMAP(m_hBitmap, NULL), 0, 0);
但它不使用 alpha 通道。
I have valid HBITMAP
handle of ARGB
type. How to draw it using GDI+?
I've tried method:
graphics.DrawImage(Bitmap::FromHBITMAP(m_hBitmap, NULL), 0, 0);
But it doesn't use alpha channel.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我有工作示例:
使用位图句柄获取信息:图像大小,位
创建并创建; 使用像素格式 PixelFormat32bppARGB 的位绘制新的 GDI+ 位图
I've got working sample:
Get info using bitmap handle: image size, bits
Create & draw new GDI+ bitmap using bits with pixel format PixelFormat32bppARGB
我在让透明渠道正常工作时遇到了类似的问题。 就我而言,我知道透明区域应该使用什么背景颜色(它是实心的)。 我使用 Bitmap.GetHBITMAP(..) 方法并传入用于透明区域的背景颜色。 与我尝试使用 LockBits 并使用 PixelFormat32bppARGB 重新创建位图以及克隆的其他尝试相比,这是一个更简单的解决方案。 就我而言,位图忽略了 Alpha 通道,因为它是从 Bitmap.FromStream 创建的。
我还遇到了一些非常奇怪的问题,图像的背景区域发生了轻微的变化。 例如,它不是纯白色,而是像 0xfff7f7 这样的灰白色。 无论我使用 JPG(具有混合颜色)还是使用 PNG 和透明颜色,情况都是如此。
请参阅我的问题和解决方案
GDI+ DrawImage of a白底JPG不是白色
I had similar issues getting my transparent channel to work. In my case, I knew what the background color should be used for the transparent area (it was solid). I used the Bitmap.GetHBITMAP(..) method and passed in the background color to be used for the transparent area. This was a much easier solution that other attempts I was trying using LockBits and re-creating the Bitmap with PixelFormat32bppARGB, as well as cloning. In my case, the Bitmap was ignoring the alpha channel since it was created from Bitmap.FromStream.
I also had some very strange problems with the background area of my image being changed slightly. For example, instead of pure white, it was off white like 0xfff7f7. This was the case whether I was using JPG (with blended colors) or with PNG and transparent colors.
See my question and solution at
GDI+ DrawImage of a JPG with white background is not white
啊...但是 .Net 不使用 HBITMAP 并且 GDI+ 是基本 Windows GDI 之上的 C++ 库,所以我假设您使用的是非 .Net C++。
GDI+ 有一个 Bitmap 类,该类有一个 FromHBITMAP() 方法。
获得 GDI+ 位图实例后,您可以将其与 GDI+ 库一起使用。
当然,如果您可以使用 .Net 用 C# 编写程序,那就容易多了。
Ah... but .Net doesn't use HBITMAP and GDI+ is a C++ library atop the basic Windows GDI, so I'm assuming you're using non-.Net C++.
GDI+ has a Bitmap class, which has a FromHBITMAP() method.
Once you have the GDI+ Bitmap instance, you can use it with the GDI+ library.
Of course, if you can write your program in C# using .Net it will be a lot easier.