Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a transparent color

Tags:

c#

colors

I have a Color, and I have a method that should return a more "transparent" version of that color. I tried the following method:

public static Color SetTransparency(int A, Color color)
{
   return Color.FromArgb(A, color.R, color.G, color.B);
}

but for some reason, no matter what the A is, the returned Color's transparency level just won't change.

Any idea?

like image 434
Graviton Avatar asked Jun 11 '10 07:06

Graviton


People also ask

Can you make a color transparent in paint?

Paint has an option of 'Transparent Selection' under 'Select' drop-down, which claims to make the background color of the selected area of the image transparent or opaque. Please follow these steps: Open image in paint. In Image Section under Select Option Select 'Transparent selection'

How do I select a transparent color in paint?

Open the image you wish to use in Paint on your computer. Next, click on Select on the left-hand side of the bar located at the top of your page. Select the Transparent Selection option from the available list.

What is transparent in color?

When making images, color can be transparent, where light passes through the colors to the color underneath, making them clearly visible. Color can be translucent, where light is partially passing through to the color underneath, making it partially visible.


2 Answers

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).

like image 59
Jon Skeet Avatar answered Nov 04 '22 05:11

Jon Skeet


Just use the correct overload of FromArgb

var color = Color.FromArgb(50, Color.Red);
like image 31
bradgonesurfing Avatar answered Nov 04 '22 06:11

bradgonesurfing