如何将透明的 Gdiplus::Bitmap 转换为 HBITMAP

问题描述

我使用以下代码创建了一个透明位图:

CRect rect(0,64,64);
Gdiplus::Bitmap bitmap(rect.Width(),rect.Height(),PixelFormat32bppARGB);
rect.DeflateRect(10,10); // Make the rectangle smaller for drawing

// write to the bitmap
Graphics graphics(&bitmap);
graphics.SetSmoothingMode(Gdiplus::SmoothingModeAntiAlias);
Gdiplus::Pen    pen(Color(255,0),6);
graphics.DrawEllipse(&pen,rect.left,rect.top,rect.Width(),rect.Height());

// convert Bitmap to HBITMAP so that hBitmap can be used to display in device context.
HBITMAP hBitmap;
bitmap.GetHBITMAP(Color(0,255,255),&hBitmap);

在上面的代码之后,我尝试将“位图”和“hBitmap”保存到 PNG 文件中。 “位图”具有透明背景。 “hBitmap”有白色背景。请参阅下面的“输出位图文件”。 hBitmap 的背景颜色取决于我传入 GetHBITMAP 函数的颜色。

我需要使用以下代码在屏幕上显示透明位图:

HDC hDcmem = CreateCompatibleDC(hDC);
HBITMAP hOldBm = (HBITMAP)SelectObject(hDcmem,hBitmap);
StretchBlt(hDC,rectDes.left,rectDes.top,rectDes.Width(),rectDes.Height(),hDcmem,rectSrc.Width(),rectSrc.Height(),SRCcopY);
DeleteDC(hDcmem);

代码只能显示不透明的HBITMAP。 有没有办法在不丢失透明度的情况下将 Bitmap 转换为 HBITMAP?

我在 stackoverflow 中尝试了许多将透明 Gdiplus::Bitmap 转换为 HBITMAP 的建议。它们似乎都不起作用。

Output bitmap file

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)