1

I cannot find any correct solution for this

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

So colorName  should have 'Brown'

Is it possible?

NoWar
  • 36,338
  • 80
  • 323
  • 498
  • 1
    About the only way I can think to do this is using reflection to loop through all the static properties of the `System.Drawing.Brushes` class and test for equality between the value of each property and the brush whose name you're trying to find. – adv12 Feb 05 '16 at 14:22
  • http://stackoverflow.com/questions/12842003/c-sharp-brush-to-string – Helic Feb 05 '16 at 14:24
  • 1
    What about `nameof(Brushes.Brown)`? – diiN__________ Feb 05 '16 at 14:27
  • SolidBrush b = Brushes.AliceBlue as SolidBrush; b.Color.Name – Helic Feb 05 '16 at 14:30

2 Answers2

1

It seems I found a way to do it.

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;
        }
NoWar
  • 36,338
  • 80
  • 323
  • 498
1

To get the desired result, you can use:

string colorName = nameof(Brushes.Brown);

Now colorName should have the value 'Brown'.

diiN__________
  • 7,393
  • 6
  • 42
  • 69