我想将Color转换为十六进制(如#FFDA2366)。 我知道,有很多像我这样的话题,但像
这样的解决方案Color color = (Color)ColorConverter.ConvertFromString(paint);
或rect.Fill = (Brush)(new BrushConverter()).ConvertFromString(paint);
不工作。
有什么想法吗?
我在类选择这样的单独文件中有 Rect 类。在主文件中,我创建了静态变量 paint - 现在。
class Rect
{
public int Width { get; set; }
public int Height { get; set; }
public int Left { get; set; }
public int Top { get; set; }
public System.Windows.Media.Brush Color { get; set; }
}
class choose
{
public void rectangle(int x, int y, int w, int h,string paint, Canvas canvas)
{
// var converter = new System.Windows.Media.BrushConverter();
//var brush = (Brush)converter.ConvertFromString("#FFFFFF90");
List<Rect> rects = new List<Rect>();
rects.Add(new Rect()
{
Width = x,
Height = y,
Left = w,
Top = h,
Color = System.Windows.Media.Brushes.Purple // instead of Purple I want to be a variable paint
});
foreach (Rect rect in rects)
{
System.Windows.Shapes.Rectangle r = new System.Windows.Shapes.Rectangle();
r.Width = rect.Width;
r.Height = rect.Width;
r.Fill = rect.Color;
Canvas.SetLeft(r, rect.Left);
Canvas.SetTop(r, rect.Top);
canvas.Children.Add(r);
}
}
}
private void rectangle_Click(object sender, RoutedEventArgs e)
{
choose r1 = new choose();
string paint = "#FFA669D1";
int x = int.Parse(beginx.Text);
int y = int.Parse(beginy.Text);
int w = int.Parse(wid.Text);
int h = int.Parse(hei.Text);
if (!((x > canvas.ActualWidth) || (y > canvas.ActualHeight) || (w > canvas.ActualWidth) ||(h > canvas.ActualHeight)))
{
r1.rectangle(x, y, w, h, paint, canvas);
}
else
{
System.Windows.MessageBox.Show("err");
}
}