I'm trying to convert a string
to a SolidColorBrush
in C#. The code I'm using is:
arrColors[arrColors.Length - 1] =
(SolidColorBrush)new BrushConverter().ConvertFromString(sLine);
where sLine is a string read from a text file. For example, sLine
might be "Black".
This code gives me a FormatException
.
Assuming all of your brushes are solid colours, you could construct a Color from a string as follows:
Then you could create a
SolidColorBrush
from that colour, like so:EDIT: If the string being converted is English but the current culture is not, you may need to use
ConvertFromInvariantString
instead, like so: