2017-05-29 69 views
-1

我將轉換顏色問題轉換爲十六進制。紅色下劃線低於System.Drawing.ColorTranslator.FromHtml("paint")rect.Color; 變量paint是靜態的 - 現在。將顏色轉換爲十六進制c#

在我看來,問題是變量的類型公共System.Drawing.SolidBrush ColorRect

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); 
    } 
} 
+2

首先擺脫引用「paint」:''System.Drawing.ColorTranslator.FromHtml(paint)'' – LocEngineer

+0

「塗料」中有什麼樣的變量? – mm8

+0

油漆是字符串 – macieqqq

回答

1

不要使用不兼容的WinForms型System.Drawing.SolidBrush一個WPF矩形的Fill財產。使用System.Windows.Media.Brush代替:

class Rect 
{ 
    ... 
    public Brush Fill { get; set; } 
} 

然後使用WPF BrushConverter類爲十六進制顏色字符串轉換爲Brush:

rect.Fill = (Brush)(new BrushConverter()).ConvertFromString(paint); 

在您的代碼示例,它應該是這樣的:

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); 
} 
+0

好吧,我做了你寫的東西。我沒有任何錯誤,但仍然沒有任何反應。 變量paint是var,我被刪除** Color = System.Drawing.ColorTranslator.FromHtml(「paint」)** – Meenti

+0

你的意思是Rectangle沒有得到它的Fill屬性集? – Clemens

+0

我想,我把油漆#FFA669D1,但仍然沒有。 – Meenti

相關問題