Set a transparent color

Just use the correct overload of FromArgb

var color = Color.FromArgb(50, Color.Red);

Well, it looks okay to me, except that you're using Color.R (etc) instead of color.R - are you sure you're actually using the returned Color rather than assuming it will change the existing color? How are you determining that the "transparency level" won't change?

Here's an example showing that the alpha value is genuinely correct in the returned color:

using System;
using System.Drawing;

class Test
{
    static Color SetTransparency(int A, Color color)
    {
        return Color.FromArgb(A, color.R, color.G, color.B);
    }
    
    static void Main()
    {
        Color halfTransparent = SetTransparency(127, Colors.Black);
        Console.WriteLine(halfTransparent.A); // Prints 127
    }
}

No surprises there. It would be really helpful if you'd provide a short but complete program which demonstrates the exact problem you're having. Are you sure that whatever you're doing with the color even supports transparency?

Note that this method effectively already exists as Color.FromArgb(int, Color).

Tags:

C#

Colors