将颜色转换为十六进制c# [英] Translate color to hexadecimal c#
问题描述
我将转换颜色转换为十六进制的问题。红色下划线低于 System.Drawing.ColorTranslator.FromHtml(paint)
和 rect.Color;
变量 paint 现在是静态的。
我认为问题出在变量类型public System.Drawing.SolidBrush Color at
Rect
class
列表< Rect> rects = new List< Rect>();
rects.Add(new Rect()
{
Width = x,
Height = y,
Left = w,
Top = h,
Fill =(System.Windows.Media.Brush)(new BrushConverter())。ConvertFromString(paint)
});
foreach(rect rect rects)
{
Rectangle r = new Rectangle
{
Width = rect.Width,
Height = rect.Width,
Fill = rect.Fill
};
Canvas.SetLeft(r,rect.Left);
Canvas.SetTop(r,rect.Top);
canvas.Children.Add(r);
}
}
class Rect
{
public int Width {get;组; }
public int Height {get;组; }
public int Left {get;组; }
public int Top {get;组; }
public System.Windows.Media.Brush Fill {get;组; }
$ b private void rectangle_Click(object sender,RoutedEventArgs e)
{
选择r1 = new choose();
var 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); (!> canvas.ActualWidth)||(y> canvas.ActualHeight)||(w> canvas.ActualWidth)||(h> canvas.ActualHeight)
if(! ))
{
r1.rectangle(x,y,w,h,paint,canvas);
$ div $解析方案
不要为WPF Rectangle的 Fill
属性使用不兼容的WinForms类型 System.Drawing.SolidBrush
。使用 System.Windows.Media.Brush
改为:
class Rect
{
...
public Brush Fill {get;组; }
}
然后使用WPF BrushConverter
class将十六进制颜色字符串转换为Brush:
rect.Fill =(Brush)(new BrushConverter() ).ConvertFromString(涂料);
在您的代码示例中,它应该看起来像这样:
var converter = new BrushConverter();
rects.Add(新Rect
{
Width = x,
Height = y,
Left = w,
Top = h ,
Fill =(Brush)converter.ConvertFromString(paint)
});
foreach(rect rect rects)
{
Rectangle r = new Rectangle
{
Width = rect.Width,
Height = rect.Width,
Fill = rect.Fill
};
Canvas.SetLeft(r,rect.Left);
Canvas.SetTop(r,rect.Top);
canvas.Children.Add(r);
}
I have problem with conversion color to hexadecimal. Red underline is below System.Drawing.ColorTranslator.FromHtml("paint")
and rect.Color;
Variable paint
is static - for now.
In my opinion the problem is in variable's type public System.Drawing.SolidBrush Color
at Rect
class
List<Rect> rects = new List<Rect>();
rects.Add(new Rect()
{
Width = x,
Height = y,
Left = w,
Top = h,
Fill = (System.Windows.Media.Brush)(new BrushConverter()).ConvertFromString(paint)
});
foreach (Rect rect in rects)
{
Rectangle r = new Rectangle
{
Width = rect.Width,
Height = rect.Width,
Fill = rect.Fill
};
Canvas.SetLeft(r, rect.Left);
Canvas.SetTop(r, rect.Top);
canvas.Children.Add(r);
}
}
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 Fill { get; set; }
}
private void rectangle_Click(object sender, RoutedEventArgs e)
{
choose r1 = new choose();
var 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);
}
}
Do not use the incompatible WinForms type System.Drawing.SolidBrush
for the Fill
property of a WPF Rectangle. Use System.Windows.Media.Brush
instead:
class Rect
{
...
public Brush Fill { get; set; }
}
Then use the WPF BrushConverter
class to convert a hexadecimal color string to a Brush:
rect.Fill = (Brush)(new BrushConverter()).ConvertFromString(paint);
In your code sample, it should look like this:
var converter = new BrushConverter();
rects.Add(new Rect
{
Width = x,
Height = y,
Left = w,
Top = h,
Fill = (Brush)converter.ConvertFromString(paint)
});
foreach (Rect rect in rects)
{
Rectangle r = new Rectangle
{
Width = rect.Width,
Height = rect.Width,
Fill = rect.Fill
};
Canvas.SetLeft(r, rect.Left);
Canvas.SetTop(r, rect.Top);
canvas.Children.Add(r);
}
这篇关于将颜色转换为十六进制c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!