How do I convert solid color to color code in c#?
if (t == 2)
{
x.Foreground = new SolidColorBrush(Colors.Green);
}
I need to get value from color code like #ffeeee
instead of color name.
I have tried :
abc.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ffaacc"));
But I can't use ColorConverter
is there is any other way?
CodePudding user response:
You can use ColorConverter, but it retuns a System.Drawing.Color. The problem is that the SolidColorBrush constructor takes a Windows.UI.Color.
There is a very simple way to convert the color from System.Drawing to Windows.UI:
public static Windows.UI.Color GetColorFromHex(string hexaColor)
{
//get the color as System.Drawing.Color
var clr = (System.Drawing.Color)new ColorConverter().ConvertFromString(hexaColor);
//convert it to Windows.UI.Color
return Windows.UI.Color.FromArgb(clr.A, clr.R, clr.G, clr.B);
}
CodePudding user response:
Color _color = System.Drawing.ColorTranslator.FromHtml("#ff0008");
abc.Foreground = _color;
You can try this, it worked with me.