首页 > 代码库 > string 到 Color 的转换示例:

string 到 Color 的转换示例:

string colorstr = "#FF4D4D4D";string hex = colorstr.ToString().Replace("#", "");byte alpha;byte pos = 0;if (hex.Length == 8){ alpha = System.Convert.ToByte(hex.Substring(pos, 2), 16); pos = 2;}else{ alpha = System.Convert.ToByte("ff", 16);}byte red = System.Convert.ToByte(hex.Substring(pos, 2), 16);pos += 2;byte green = System.Convert.ToByte(hex.Substring(pos, 2), 16);pos += 2;byte blue = System.Convert.ToByte(hex.Substring(pos, 2), 16);Color color = Color.FromArgb(alpha, red, green, blue);