【发布时间】:2010-04-23 19:45:01
【问题描述】:
我有两个 System.Windows.Media.Color(a 和 b),需要获取 a 并将 置于 b 上以模拟 透明度。在我的合并方法中使用:
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));
}
我在 Mix Method 中发现一个舍入错误,使用 Math.Round() 时解决:
public static Color Mix(Color from, Color to, float percent) { float amountFrom = 1.0f - 百分比;
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));
}
【问题讨论】:
-
它有效,但要正确,您需要在转换前后应用伽马校正。