tags:

views:

40

answers:

1

I have two System.Windows.Media.Color (a and b)and need to get a and put over b to simulate tranparency. to use in my merge method:

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;
}

Help Thank's!!

Solution:

        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));
        }

I found one rounding error in Mix Method, solve when use Math.Round():

public static Color Mix(Color from, Color to, float percent) { 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));
}
+1  A: 

Found this article featuring the following method:

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));
}

Call it like this:

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

You might have to play with the function (maybe even alter it) a little bit, but i think that it can get you exactly the result you're looking for.

Paul Sasik
dont't work!! thanks!!
CrazyJoe
Try this one instead.
Paul Sasik
Work Perfectly man, thank´s a lot!!! I Edit post do show completely solution!!
CrazyJoe