我想在代码中设置椭圆对象的颜色。目前,我是通过使用SolidColorBrush方法来实现的。是否有一种方式可以像CSS中那样插入十六进制的颜色值?
这是我正在使用的代码:
ellipse.Fill = new SolidColorBrush(Colors.Yellow);
我想在代码中设置椭圆对象的颜色。目前,我是通过使用SolidColorBrush方法来实现的。是否有一种方式可以像CSS中那样插入十六进制的颜色值?
这是我正在使用的代码:
ellipse.Fill = new SolidColorBrush(Colors.Yellow);
这样做可以起作用
ellipse.Fill =
new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF00DD"));
Color.FromRgb
方法。byte r = 255;
byte g = 0;
byte b = 221;
ellipse.Fill = new SolidColorBrush(Color.FromRgb(r,g,b));
From MSDN
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
// Describes the brush's color using RGB values.
// Each value has a range of 0-255.
mySolidColorBrush.Color = Color.FromArgb(255, 0, 0, 255);
myRgbRectangle.Fill = mySolidColorBrush;
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
// Describes the brush's color using RGB HEX values.
// Each value has a range of 0-255. Use 0x for HEX numbers
mySolidColorBrush.Color = Color.FromArgb(255, 0xFF, 0xC0, 0xD0);
myRgbRectangle.Fill = mySolidColorBrush;
使用十六进制值:
your_contorl.Color = DirectCast(ColorConverter.ConvertFromString("#D8E0A627"), Color)
另一个,小巧、快速且实用:
public static Color ToColor(this uint argb)
{
return Color.FromArgb((byte)((argb & -16777216) >> 0x18),
(byte)((argb & 0xff0000) >> 0x10),
(byte)((argb & 0xff00) >> 8),
(byte)(argb & 0xff));
}
在代码中使用:
SolidColorBrush scb = new SolidColorBrush (0xFFABCDEF.ToColor());
我认为这会起作用,因为它对文本框有效。
var bc = new BrushConverter();
textRichTextBoxEditor.Foreground = (Brush)bc.ConvertFrom("#FF97315A");