how to change transparency of a color in c#

前端 未结 4 846
天命终不由人
天命终不由人 2021-02-12 11:38

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\", \"<

相关标签:
4条回答
  • 2021-02-12 11:58

    You can set with this function

        static Color SetTransparency(int A, Color color)
        {
            return Color.FromArgb(A, color.R, color.G, color.B);
        }
    
    0 讨论(0)
  • 2021-02-12 11:58

    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);
    
    0 讨论(0)
  • 2021-02-12 12:03

    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);
    
    0 讨论(0)
  • 2021-02-12 12:12

    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
    
    0 讨论(0)
提交回复
热议问题