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

时间:2016-12-10 18:53:20

标签: c++ mfc gdi+

我正在尝试使用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);

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

1 个答案:

答案 0 :(得分:3)

要使用Alpha混合绘制图像,请使用所需的Alpha通道声明Gdiplus::ImageAttributesGdiplus::ColorMatrix

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中使用。如果您确实使用它,请务必在不再需要ReleaseDC(pDC)时致电pDC。或者只使用具有自动清理功能的CClientDC dc(this)。如果在OnPaint中完成绘画,那么使用CPaintDC也可以进行自动清理:

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