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

前端 未结 16 772
不思量自难忘°
不思量自难忘° 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 06:53

    If you want to do it with a Windows Store App, following by @Hans Kesting and @Jink answer:

        string colorcode = "#FFEEDDCC";
        int argb = Int32.Parse(colorcode.Replace("#", ""), NumberStyles.HexNumber);
        tData.DefaultData = Color.FromArgb((byte)((argb & -16777216) >> 0x18),
                              (byte)((argb & 0xff0000) >> 0x10),
                              (byte)((argb & 0xff00) >> 8),
                              (byte)(argb & 0xff));
    
    0 讨论(0)
  • 2020-11-22 06:53

    You can see Silverlight/WPF sets ellipse with hexadecimal colour for using a hex value:

    your_contorl.Color = DirectCast(ColorConverter.ConvertFromString("#D8E0A627"), Color)
    
    0 讨论(0)
  • 2020-11-22 06:55

    Assuming you mean the HTML type RGB codes (called Hex codes, such as #FFCC66), use the ColorTranslator class:

    System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml("#FFCC66");
    

    If, however you are using an ARGB hex code, you can use the ColorConverter class from the System.Windows.Media namespace:

    Color col = ColorConverter.ConvertFromString("#FFDFD991") as Color;
    //or      = (Color) ColorConverter.ConvertFromString("#FFCC66") ;
    
    0 讨论(0)
  • 2020-11-22 06:55

    This post has become the goto for anyone trying to convert from a hex color code to a system color. Therefore, I thought I'd add a comprehensive solution that deals with both 6 digit (RGB) and 8 digit (ARGB) hex values.

    By default, according to Microsoft, when converting from an RGB to ARGB value

    The alpha value is implicitly 255 (fully opaque).

    This means by adding FF to a 6 digit (RGB) hex color code it becomes an 8 digit ARGB hex color code. Therefore, a simple method can be created that handles both ARGB and RGB hex's and converts them to the appropriate Color struct.

        public static System.Drawing.Color GetColorFromHexValue(string hex)
        {
            string cleanHex = hex.Replace("0x", "").TrimStart('#');
    
            if (cleanHex.Length == 6)
            {
                //Affix fully opaque alpha hex value of FF (225)
                cleanHex = "FF" + cleanHex;
            }
    
            int argb;
    
            if (Int32.TryParse(cleanHex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out argb))
            {
                return System.Drawing.Color.FromArgb(argb);
            }
    
            //If method hasn't returned a color yet, then there's a problem
            throw new ArgumentException("Invalid Hex value. Hex must be either an ARGB (8 digits) or RGB (6 digits)");
    
        }
    

    This was inspired by Hans Kesting's answer.

    0 讨论(0)
  • 2020-11-22 06:59

    in asp.net:

    color_black = (Color)new ColorConverter().ConvertFromString("#FF76B3");
    
    0 讨论(0)
  • 2020-11-22 07:00

    There is also this neat little extension method:

    static class ExtensionMethods
    {
        public static Color ToColor(this uint argb)
        {
            return Color.FromArgb((byte)((argb & -16777216)>> 0x18),      
                                  (byte)((argb & 0xff0000)>> 0x10),   
                                  (byte)((argb & 0xff00) >> 8),
                                  (byte)(argb & 0xff));
        }
    }
    

    In use:

    Color color = 0xFFDFD991.ToColor();
    
    0 讨论(0)
提交回复
热议问题