我有两个System.Windows.Media.Color(a和b)并且需要获取并将通过b来模拟透明性。在我的合并方法中使用:合并2种颜色以制成透明的Ovelap?
public static Image Merge(Image a,Image b)
{
for(int x=0;x < b.Width;x++)
{
for (int y = 0; y < b.Height; y++)
{
a.SetPixel(x, y, b.GetPixel(x, y));
}
}
return a;
}
帮助谢谢!
解决方案:
public static Image Merge(Image a,Image b)
{
for(int x=0;x < b.Width;x++)
{
for (int y = 0; y < b.Height; y++)
{
a.SetPixel(x, y, Mix(a.GetPixel(x, y), b.GetPixel(x, y), .5f));
//a.SetPixel(x, y,b.GetPixel(x, y));
}
}
return a;
}
public static Color Mix(Color from, Color to, float percent)
{
float amountFrom = 1.0f - percent;
return Color.FromArgb(
(byte)(from.A * amountFrom + to.A * percent),
(byte)(from.R * amountFrom + to.R * percent),
(byte)(from.G * amountFrom + to.G * percent),
(byte)(from.B * amountFrom + to.B * percent));
}
我发现了一个舍入误差在混法,解决在使用时Math.Round():
公共静态颜色混合(从颜色,颜色到,浮动百分比) float amountFrom = 1.0f - percent;
return Color.FromArgb(
(byte)Math.Round(from.A * amountFrom + to.A * percent),
(byte)Math.Round(from.R * amountFrom + to.R * percent),
(byte)Math.Round(from.G * amountFrom + to.G * percent),
(byte)Math.Round(from.B * amountFrom + to.B * percent));
}
它的工作原理,但要正确,您需要在转换前后应用伽马校正。 – Ross 2010-04-23 11:25:42