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

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

        private Color FromHex(string hex)
        {
            if (hex.StartsWith("#"))
                hex = hex.Substring(1);
    
            if (hex.Length != 6) throw new Exception("Color not valid");
    
            return Color.FromArgb(
                int.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber),
                int.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.HexNumber),
                int.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber));
        }
    

提交回复
热议问题