0

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);
    }
}
Meenti
  • 43
  • 1
  • 2
  • 6

1 Answers1

2

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);
}
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • Ok, I did what you write.I don't have any errors but still nothing happens. The variable paint is var and I deleted **Color = System.Drawing.ColorTranslator.FromHtml("paint")** – Meenti Jun 01 '17 at 08:53
  • You mean the Rectangle doesn't get its Fill property set? – Clemens Jun 01 '17 at 08:55
  • I think, I put instead paint #FFA669D1, but still nothing. – Meenti Jun 01 '17 at 08:58
  • Hard to tell without seeing what you're actually doing now. Anyway, see my edit. – Clemens Jun 01 '17 at 09:03
  • Now, I have red underline below **r** and **Fill** = rect.Fill. The error says that " Cannot convert System.Drawing.Rectangle to System.Windows.UIElement" - to the variable **r** and " Element Rectangle has no definition Fill" - to the position Fill. I update my code. – Meenti Jun 01 '17 at 09:26
  • Remove all `using System.Drawing...` lines from your code and use the correct WPF namespaces, e.g. `using System.Windows.Shapes;` – Clemens Jun 01 '17 at 10:06