2016-02-05 42 views
0

我找不到這個如何System.Drawing.Brush轉換爲字符串

string colorName = ...converting... Brushes.Brown; 

So colorName should have 'Brown' 

任何正確的解決方案,可以嗎?

+1

關於唯一可以這樣做的方法是使用反射循環遍歷System.Drawing.Brushes類的所有靜態屬性,並測試每個屬性的值與名稱爲'重新嘗試尋找。 – adv12

+0

http://stackoverflow.com/questions/12842003/c-sharp-brush-to-string – Helic

+1

'nameof(Brushes.Brown)''怎麼樣? –

回答

0

看來我找到了一個辦法。

public string GetColorName(Brush brush) 
     { 
      string name = "Unknown"; 
      Color c = new Pen(brush).Color; 

      foreach (KnownColor kc in Enum.GetValues(typeof(KnownColor))) 
      { 
       Color known = Color.FromKnownColor(kc); 
       if (c.ToArgb() == known.ToArgb()) 
       { 
        name = known.Name; 
        break; 
       } 
      } 

      return name; 
     } 
1

要獲得期望的結果,你可以使用:

string colorName = nameof(Brushes.Brown); 

現在colorName應有的價值'Brown'