How to draw ARGB bitmap using GDI+?

依然范特西╮ 提交于 2019-12-04 03:42:12

问题


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.


回答1:


I've got working sample:

// 1. Get info using bitmap handle: image size, bits
BITMAP bmpInfo;
::GetObject(m_hBitmap, sizeof(BITMAP), &bmpInfo);
int cxBitmap = bmpInfo.bmWidth;
int cyBitmap = bmpInfo.bmHeight;
void* bits = bmpInfo.bmBits;

// 2. Create & draw new GDI+ bitmap using bits with pixel format PixelFormat32bppARGB
Gdiplus::Graphics graphics(dcMemory);
Gdiplus::Bitmap bitmap(cxBitmap, cyBitmap, cxBitmap*4, PixelFormat32bppARGB, (BYTE*)bits);
graphics.DrawImage(&bitmap, 0, 0);




回答2:


.NET offers the static Image.FromHbitmap method. You can then use this image and invoke DrawImage on the target Graphics object.




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/182988/how-to-draw-argb-bitmap-using-gdi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!