gpt4 book ai didi

c++ - 如何使用 GDI+ 显示不透明度为 25% 的 PNG 图像? (MFC)

转载 作者:行者123 更新时间:2023-11-30 02:28:08 36 4
gpt4 key购买 nike

我正在尝试使用 GDI+、MFC 输出 PNG 图像。我想以 25% 的不透明度输出它。下面是在 x=10, y=10 上输出 PNG 图片的方法:

    CDC *pDC =GetDC();
Graphics graphics(pDC->m_hDC);
Image image(L"test1.png", FALSE);
graphics.DrawImage(&image, 10, 10);

但是我不知道如何让它变成半透明的。有什么想法吗?

最佳答案

要使用 alpha 混合绘制图像,请声明 Gdiplus::ImageAttributesGdiplus::ColorMatrix具有所需的 alpha channel :

float alpha = 0.25f;
Gdiplus::ColorMatrix matrix =
{
1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, alpha, 0,
0, 0, 0, 0, 1
};

Gdiplus::ImageAttributes attrib;
attrib.SetColorMatrix(&matrix);
graphics.DrawImage(&image,
Gdiplus::Rect(10, 10, image.GetWidth(), image.GetHeight()),
0, 0, image.GetWidth(), image.GetHeight(), Gdiplus::UnitPixel, &attrib);

另请参阅:Using a Color Matrix to Transform a Single Color

请注意,GetDC() 通常不会在 MFC 中使用。如果确实要使用它,请务必在不再需要 pDC 时调用 ReleaseDC(pDC)。或者简单地使用具有自动清理功能的 CClientDC dc(this)。如果绘画是在 OnPaint 中完成的,那么使用 CPaintDC 也有自动清理功能:

void CMyWnd::OnPaint()
{
CPaintDC dc(this);
Gdiplus::Graphics graphics(dc);
...
}

关于c++ - 如何使用 GDI+ 显示不透明度为 25% 的 PNG 图像? (MFC),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41079112/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com