1. 程式人生 > >C#十六進制顏色與Color對象的轉換

C#十六進制顏色與Color對象的轉換

sub convert tran div back draw lin style 十進制轉十六進制

//把十六進制顏色轉化為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);

C#十六進制顏色與Color對象的轉換