Mega Code Archive

 
Categories / C# / 2D Graphics
 

Get Color From String

using System; using System.IO; using System.Text; using System.Windows.Media; using System.Windows.Media.Imaging; public static class Tool {     public static Color GetColorFromString(string colorString)     {         Color result = Colors.Black;         try         {             colorString = colorString.ToUpper().Trim('#');             string a = "0x" + colorString.Substring(0, 2);             string r = "0x" + colorString.Substring(2, 2);             string g = "0x" + colorString.Substring(4, 2);             string b = "0x" + colorString.Substring(6, 2);             byte A = Convert.ToByte(a, 16);             byte R = Convert.ToByte(r, 16);             byte G = Convert.ToByte(g, 16);             byte B = Convert.ToByte(b, 16);             result = Color.FromArgb(A, R, G, B);         }         catch { }         return result;     } }