如何在c#中更改颜色的透明度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17753043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to change transparency of a color in c#
提问by Shruti Kapoor
I am using SSRS reportviewer to generate a report using objects. In my program, I am asking the user to input a string of commonly known colors such as "Red"
, "Blue"
, etc. I would like to then generate three shades of this color and use this color to fill an area chart in my report. I do so my changing the opacity (alpha) of the color.
我正在使用 SSRS 报告查看器使用对象生成报告。在我的程序中,我要求用户输入一串众所周知的颜色,例如"Red"
, "Blue"
等。然后我想生成这种颜色的三种阴影,并使用这种颜色来填充我的报告中的面积图。我愿意所以我改变了颜色的不透明度(alpha)。
This is my code that converts the string to color:
这是我将字符串转换为颜色的代码:
newitem.ChartColor = "red";
Color mycolor = Color.FromName(newitem.ChartColor);
However, now I would like to generate two more colors with same shade as red but different alpha (opacity) so that they appear lighter, something like #56FF0000
但是,现在我想再生成两种颜色,其阴影与红色相同但alpha(不透明度)不同,以便它们看起来更亮,例如 #56FF0000
I tried passing a value to the A
property of Color however, it is read-only.
我尝试将值传递给A
Color的属性,但是它是只读的。
Any help appreciated.
任何帮助表示赞赏。
采纳答案by Ma3x
There is a method that does exactly what you need Color.FromArgb(int alpha, Color baseColor).
有一种方法可以完全满足您的需要Color.FromArgb(int alpha, Color baseColor)。
Valid alpha
values are 0 through 255. Where 255 is the most opaque color and 0 a totally transparent color.
有效alpha
值为 0 到 255。其中 255 是最不透明的颜色,0 是完全透明的颜色。
Use example
使用示例
Color newColor = Color.FromArgb(newAlpha, mycolor);
回答by Aghilas Yakoub
You can set with this function
您可以使用此功能进行设置
static Color SetTransparency(int A, Color color)
{
return Color.FromArgb(A, color.R, color.G, color.B);
}
回答by Jazimov
I think what needs to be included among these answers is that the alpha value indicates how transparent the color is with 0 being the most transparent and with 255 being the most opaque. Here is a summary:
我认为这些答案中需要包括的是 alpha 值表示颜色的透明度,0 表示最透明,255 表示最不透明。这是一个总结:
A L P H A V A L U E
0 [<--- most transparent] ... ... ... [most opaque --->] 255
回答by Jim Berg
I created a handy extension method.
我创建了一个方便的扩展方法。
public static class ColorExtensions
{
...
public static Color WithA(this Color color, int newA) => Color.FromArgb(newA,color);
}
Usage:
用法:
newitem.ChartColor = "red";
Color mycolor = Color.FromName(newitem.ChartColor);
Color myColorAlt1 = myColor.WithA(0x56);
Color myColorAlt2 = myColor.WithA(0x28);
or, if you needed it right away:
或者,如果您立即需要它:
Color mycolor = Color.FromName(newitem.ChartColor).WithA(0x56);