【发布时间】:2019-02-15 01:50:21
【问题描述】:
我需要基于纯色(背景)和半透明图像的合并生成一个新图像。 所有图像都将是半透明的,因此它们都将与纯色混合。
我试过了:
private Image BlendImageWithWindowBackgoundColor(Image pImage, float pColorOpacity)
{
Image mResult = null;
if (pImage != null)
{
ColorMatrix matrix = new ColorMatrix(new float[][]{
new float[] {1F, 0, 0, 0, 0},
new float[] {0, 1F, 0, 0, 0},
new float[] {0, 0, 1F, 0, 0},
new float[] {0, 0, 0, pColorOpacity, 0}, //opacity in rage [0 1]
new float[] {0, 0, 0, 0, 1F}});
ImageAttributes imageAttributes = new ImageAttributes();
imageAttributes.SetColorMatrix(matrix);
imageAttributes.SetWrapMode(WrapMode.TileFlipXY);
mResult = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(mResult);
g.Clear(Color.Red); //<--This is the color i want merged as background!
g.CompositingMode = CompositingMode.SourceCopy;
g.CompositingQuality = CompositingQuality.HighQuality;
g.DrawImage(pImage, new Rectangle(0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height), 0, 0, pImage.Width, pImage.Height, GraphicsUnit.Pixel, imageAttributes);
}
return mResult;
}
如果我尝试 pOpacity 0.5 看不到红色,当 pOpacity 为 0 时它返回黑色。
即使我尝试使用 g.CompositingMode = CompositingMode.SourceOver 它也只会生成半透明图像,忽略红色背景。
【问题讨论】:
-
如果你只是合并,那你为什么要使用
SetWrapMode?
标签: c# image-processing gdi+