Reputation: 31
I created a Bitmap like following in C# (for example):
Image image = Image.FromFile("xxx.png");
Bitmap bitmap = new(image.Width, image.Height, PixelFormat.Format32bppArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.DrawImage(image, 0, 0, image.Width, image.Height);
And now I need to pass this Bitmap to C++ and I meet problems.
I tried to write a convert function like:
Gdiplus::Bitmap* Utils::BitmapConverter::Convert(Drawing::Bitmap^ managedBitmap)
{
IntPtr hBitmap = managedBitmap->GetHbitmap();
return Gdiplus::Bitmap::FromHBITMAP((HBITMAP)hBitmap.ToPointer(), NULL);
}
But it doesn't work perfectly: something like alpha channel is lost.
What should I actually change in my convert function?
Upvotes: 1
Views: 53
Reputation: 405
Make sure the Bitmap is created with PixelFormat.Format32bppArgb (which supports the alpha channel). Try using GetHbitmap(Color::Transparent) to keep the alpha channel when making the HBITMAP (don't forget to delete the HBITMAP handle to prevent memory leaks).
Also, depending on how you pass the Bitmap from C# to C++ (like via PInvoke or COM), you might need extra steps to transfer the bitmap correctly. That is, assuming you already have the interop method set up.
Upvotes: 0