How to draw an ARGB bitmap using GDI +?

I have a valid HBITMAP handle of type ARGB. How to draw it using GDI +?

I tried the method: graphics.DrawImage (Bitmap :: FromHBITMAP (m_hBitmap, NULL), 0, 0); But he does not use the alpha channel.

+4
source share
4 answers

I have a working sample:

//1. Get information using the bitmap handler: image size, bit

BITMAP bmpInfo;
::GetObject(m_hBitmap, sizeof(BITMAP), &bmpInfo);
int cxBitmap = bmpInfo.bmWidth;
int cyBitmap = bmpInfo.bmHeight;
void* bits = bmpInfo.bmBits;

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

+6
source

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

0
source

Ah ... but .Net does not use HBITMAP, and GDI + is a C ++ library based on Windows GDI, so I assume you are using non-.NET C ++.

GDI + has a Bitmap class that has a FromHBITMAP () method.

Once you have a 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 much easier.

0
source

I had similar problems in order for my transparent channel to work. In my case, I knew what background color should be used for the transparent area (it was solid). I used the Bitmap.GetHBITMAP (..) method and passed in the background color that will be used for the transparent area. It was much easier than other attempts that I tried to use LockBits and recreate Bitmap with PixelFormat32bppARGB, as well as cloning. In my case, Bitmap ignored the alpha channel since it was created from Bitmap.FromStream.

I also had some very strange problems with changing the background area of ​​my image. For example, instead of pure white, it was white, like 0xfff7f7. This was the case if I used JPG (with mixed colors) or with PNG and transparent colors.

See my question and solution at

GDI + DrawImage JPG with white background not white

0
source

All Articles