how to change transparency of a color in c#

你。 提交于 2019-12-09 08:35:24

问题


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.

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

I tried passing a value to the A property of Color however, it is read-only.

Any help appreciated.


回答1:


There is a method that does exactly what you need 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.

Use example

Color newColor = Color.FromArgb(newAlpha, mycolor);



回答2:


You can set with this function

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



回答3:


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:

                     A L P H A    V A L U E
0 [<--- most transparent]  ... ... ... [most opaque --->] 255


来源:https://stackoverflow.com/questions/17753043/how-to-change-transparency-of-a-color-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!