tags:

views:

1914

answers:

3

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.

A: 

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

Konrad Rudolph
This method doesn't draw transparent color. Here is the question. How to notify Gdi+ I want to draw exactly ARGB bitmap?
GDI+ Bitmap object has a constructor that takes Width, Height and PixelFormat as parameters. Once constructed, the bitmap will be in the desired bit depth and include an Alpha channel if requested.
Jeff B
+1  A: 

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.

Jeff B
+2  A: 

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);