Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用GDI+;显示不透明度为25%的PNG图像;?(MFC)_C++_Mfc_Gdi+ - Fatal编程技术网

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

C++ 如何使用GDI+;显示不透明度为25%的PNG图像;?(MFC),c++,mfc,gdi+,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); 但我不知道如何使它半透明。有什么想法吗?要使用alpha混合、声明和所需的alpha

我正在尝试使用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混合、声明和所需的alpha通道绘制图像:

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);
另见:

请注意,
GetDC()
通常不在MFC中使用。如果确实使用了它,请确保在不再需要
pDC
时调用
ReleaseDC(pDC)
。或者只需使用具有自动清理功能的
CClientDC(this)
。如果在
OnPaint
中完成绘制,则使用
CPaintDC
进行自动清理:


您提出了两个不同的问题:标题表明,您希望图像的alpha通道产生任何效果,而问题主体似乎只希望以25%的不透明度渲染整个图像。您需要哪一个?对于Windows 8及以上版本,一个(更简单、更高效)的替代方法是为映像创建一个新的映像,并让系统来完成繁重的工作。
void CMyWnd::OnPaint()
{
    CPaintDC dc(this);
    Gdiplus::Graphics graphics(dc);
    ...
}