How do I get the color from a hexadecimal color code using .NET?

前端 未结 16 773
不思量自难忘°
不思量自难忘° 2020-11-22 06:45

How can I get a color from a hexadecimal color code (e.g. #FFDFD991)?

I am reading a file and am getting a hexadecimal color code. I need to create the

相关标签:
16条回答
  • 2020-11-22 07:14

    If you don't want to use the ColorTranslator, you can do it in easily:

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

    The colorcode is just the hexadecimal representation of the ARGB value.

    EDIT

    If you need to use 4 values instead of a single integer, you can use this (combining several comments):

    string colorcode = "#FFFFFF00";    
    colorcode = colorcode.TrimStart('#');
    
    Color col; // from System.Drawing or System.Windows.Media
    if (colorcode.Length == 6)
        col = Color.FromArgb(255, // hardcoded opaque
                    int.Parse(colorcode.Substring(0,2), NumberStyles.HexNumber),
                    int.Parse(colorcode.Substring(2,2), NumberStyles.HexNumber),
                    int.Parse(colorcode.Substring(4,2), NumberStyles.HexNumber));
    else // assuming length of 8
        col = Color.FromArgb(
                    int.Parse(colorcode.Substring(0, 2), NumberStyles.HexNumber),
                    int.Parse(colorcode.Substring(2, 2), NumberStyles.HexNumber),
                    int.Parse(colorcode.Substring(4, 2), NumberStyles.HexNumber),
                    int.Parse(colorcode.Substring(6, 2), NumberStyles.HexNumber));
    

    Note 1: NumberStyles is in System.Globalization.
    Note 2: please provide your own error checking (colorcode should be a hexadecimal value of either 6 or 8 characters)

    0 讨论(0)
  • 2020-11-22 07:17

    WPF:

    using System.Windows.Media;
    
    //hex to color
    Color color = (Color)ColorConverter.ConvertFromString("#7AFF7A7A");
    
    //color to hex
    string hexcolor = color.ToString();
    
    0 讨论(0)
  • 2020-11-22 07:18

    I needed to convert a HEX color code to a System.Drawing.Color, specifically a shade of Alice Blue as a background on a WPF form and found it took longer than expected to find the answer:

    using System.Windows.Media;
    

    --

    System.Drawing.Color myColor = System.Drawing.ColorTranslator.FromHtml("#EFF3F7");
    this.Background = new SolidColorBrush(System.Windows.Media.Color.FromArgb(myColor.A, myColor.R, myColor.G, myColor.B));
    
    0 讨论(0)
  • 2020-11-22 07:18

    I used ColorDialog in my project. ColorDialog sometimess return "Red","Fhushia" and sometimes return "fff000". I solved this problem like this maybe help someone.

            SolidBrush guideLineColor;
            if (inputColor.Any(c => char.IsDigit(c)))
            {
                string colorcode = inputColor;
                int argbInputColor = Int32.Parse(colorcode.Replace("#", ""), NumberStyles.HexNumber);
                 guideLineColor = new SolidBrush(Color.FromArgb(argbInputColor));
    
            }
            else
            {
                Color col = Color.FromName(inputColor);
                 guideLineColor = new SolidBrush(col);
            }
    

    InputColor is the return value from ColorDialog.

    Thanks everyone for answer this question.It's big help to me.

    0 讨论(0)
提交回复
热议问题