基本上,当直接在纯色表面上绘制时,具有 alpha 值的图像可以正确绘制,但是当它到达窗口透明的区域时,具有 alpha 值的 PNG 开始具有白色背景。
希望我的代码能帮助你们理解发生了什么。非常感谢所有帮助。
GDI+绘图
Image indicator(L"resources\\images\\floating_icon [a] 1.png", FALSE);
g.DrawImage(&indicator, 0, 0);
TextureBrush myTextureBrush(&indicator);
g.FillRectangle(&myTextureBrush, WindowPadding, 50, WindowDim.right - WindowPadding * 2, WindowPadding);
窗口属性
hWnd = CreateWindowEx(0, szWindowClass, szTitle, WS_POPUP | WS_OVERLAPPED,
0, 0, WindowDim.right, WindowDim.bottom, NULL, NULL, hInstance, NULL);
SetLayeredWindowAttributes(hWnd, RGB(255, 255, 255), 0,
LWA_COLORKEY);
最佳答案
如果你的底层位图是纯白色 (255,255,255),那么当你在其上绘制 alpha 图像时,你最终会得到一个不是纯白色的颜色(除非像素完全透明) - 的每个像素图像与背景的底层颜色混合。
这样做的问题是你的色键只会使纯白色像素透明,其他任何东西都会是实心的。
解决方案是切换到对分层窗口使用每像素 alpha。
关于c++ - 如何在 GDI+ 中绘制 PNG 并保持透明度/Alpha channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18845105/