Convert color to byte value

后端 未结 4 916
南方客
南方客 2021-01-28 15:28

In C#, how can I convert a Color object into a byte value?

For example, the color #FFF would be converted to the value 255

相关标签:
4条回答
  • 2021-01-28 16:00

    Try this,

    string colorcode = "#FFFFFF00";
    int argb = Int32.Parse(colorcode.Replace("#", ""), NumberStyles.HexNumber);
    Color clr = Color.FromArgb(argb);
    

    also see this How to get Color from Hexadecimal color code using .NET?

    0 讨论(0)
  • 2021-01-28 16:01

    You could use the ColorTranslator.FromHtml function:

    Color color = ColorTranslator.FromHtml("#FFF");
    
    0 讨论(0)
  • 2021-01-28 16:06

    You can get the byte values of a .NET Color object with:

    byte red = color.R;
    byte green = color.G;
    byte blue = color.B;
    

    That gives you 3 bytes. I don't know how you expect to get a single byte value. Colors are (AFAIK) almost never represented by single bytes.

    0 讨论(0)
  • 2021-01-28 16:12

    You can use ConvertFromString() method from ColorConverter class.

    Attempts to convert a string to a Color.

    Return Value
    Type: System.Object
    A Color that represents the converted text.
    

    ColorConverter c = new ColorConverter();
    Color color = (Color)c.ConvertFromString("#FFF");
    Console.WriteLine(color.Name);
    
    0 讨论(0)
提交回复
热议问题