C#十六进制颜色与Color对象的转换
//把十六进制颜色转化为color对象 ColorTranslator.FromHtml("#FF0000") 或 ColorTranslator.FromHtml("Red"); //把color对象转化为十六进制颜色 ColorTranslator.ToHtml(Color.FromArgb(255,255,255)) 或 ColorTranslator.ToHtml(Color.Red); //十进制转二进制 Console.WriteLine(Convert.ToString(69, 2)); //十进制转八进制 Console.WriteLine(Convert.ToString(69, 8)); //十进制转十六进制 Console.WriteLine(Convert.ToString(69, 16)); //二进制转十进制 Console.WriteLine(Convert.ToInt32(”100111101″, 2)); //八进制转十进制 Console.WriteLine(Convert.ToInt32(”76″, 8)); //十六进制转十进制 Console.WriteLine(Convert.ToInt32(”FF”, 16)); string color_s = ColorTranslator.ToHtml(g.Pen.Color); color_s = color_s.Substring(1, color_s.Length - 1); g.Pen.Color = ColorTranslator.FromHtml("#"+Convert.ToString(Convert.ToInt32(color_s, 16)+40000, 16));
exp:
Color Color1 = System.Drawing.ColorTranslator.FromHtml("#EEEEEE"); Color Color2 = System.Drawing.ColorTranslator.FromHtml("red"); /* ** Convert back: */ string Color1a = System.Drawing.ColorTranslator.ToHtml(Color1); // --> "#EEEEEE" string Color2a = System.Drawing.ColorTranslator.ToHtml(Color2); // --> "Red" string Color2b = String.Format("#{0:X2}{1:X2}{2:X2}", Color2.R, Color2.G, Color2.B);