【问题标题】:Merge 2 Colors to make a tranparent Ovelap?合并 2 种颜色以制作透明叠加?
【发布时间】: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));
}

【问题讨论】:

  • 它有效,但要正确,您需要在转换前后应用伽马校正。

标签: c# .net image


【解决方案1】:

发现 this article 具有以下方法:

public static Color Mix(Color from, Color to, float percent)
{
    float amountFrom = 1.0f - percent;

    return Color.FromArgb(
    (int)(from.A * amountFrom + to.A * percent),
    (int)(from.R * amountFrom + to.R * percent),
    (int)(from.G * amountFrom + to.G * percent),
    (int)(from.B * amountFrom + to.B * percent));
}

这样称呼它:

a.SetPixel(x, y, Mix(a.GetPixel(x, y), b.GetPixel(x, y), .5f));

您可能需要稍微使用一下该功能(甚至可能对其进行更改),但我认为它可以为您提供您正在寻找的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-06
    • 2014-02-20
    • 2018-08-24
    • 1970-01-01
    • 2017-09-23
    • 1970-01-01
    • 2014-02-04
    • 1970-01-01
    相关资源
    最近更新 更多