tags:

views:

1932

answers:

1

When I create a new Gdiplus::Bitmap using the Bitmap::FromHBITMAP function, the resulting Bitmap is opaque - none of the partial transparency from the original HBITMAP is preserved.

Is there a way to create a Gdiplus::Bitmap from an HBITMAP which brings across the alpha channel data?

+1  A: 

It turns out that GDI+ never brings across the alpha channel when creating a Bitmap from an HBITMAP.

The answer is to:

  • Use GetObject passing in a BITMAP and the HBITMAP, to get the width and height (and if the input bitmap is a DIB, the pixel data) of the input HBITMAP.
  • Create a Bitmap of the correct size with 32 bit PARGB pixel format.
  • Use LockBits to get hold of the pixelData memory of your new Bitmap.
  • If you got the pixels from GetObject, copy the ARGB values across using memcpy.
  • Call UnlockBits on the new Bitmap.

In my case, the format of the input HBITMAP is correct for doing a straight memcpy from input bitmap pixel data to the new Bitmap pixel data.

If you didnt get the input pixel data from GetObject, use GetDIBits to get a copy in the correct format.

mackenir