1

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.

TaW
  • 53,122
  • 8
  • 69
  • 111
Niels Bril
  • 21
  • 1
  • 1
  • 4

2 Answers2

8

Assuming all of your brushes are solid colours, you could construct a Color from a string as follows:

Color color = (Color)ColorConverter.ConvertFromString(sLine);

Then you could create a SolidColorBrush from that colour, like so:

SolidColorBrush brush = new SolidColorBrush(color);

EDIT: If the string being converted is English but the current culture is not, you may need to use ConvertFromInvariantString instead, like so:

ColorConverter converter = new ColorConverter();
Color color = (Color)converter.ConvertFromInvariantString(sLine);
Tom Galvin
  • 861
  • 7
  • 12
  • Thanks, but is there any using statement for Color.FromName i need to use? Now it gives me that 'System.Windows.Media.Color' does not contain a definition for 'FromName'. – Niels Bril Dec 04 '14 at 20:21
  • Yes @TaW. Fact is that there are several colors that go into my array. So i cant use Color.FromName("Black") or something like that. – Niels Bril Dec 04 '14 at 20:23
  • @NielsBril: I've adapted the solution to work for the WPF classes, I didn't realise that was what you were using. – Tom Galvin Dec 04 '14 at 20:26
  • @NielsBril I've edited my solution again; I'm assuming from your source code example that the wrong CultureInfo may be used. In that case then the invariant culture should be used instead. – Tom Galvin Dec 04 '14 at 20:36
  • @Quackmatic still not solved :( i can't use the ConvertFromInvariantString method. is there any way to send you the project? – Niels Bril Dec 04 '14 at 20:47
  • It seems `ConvertFromInvariantString` is an instance method (for some reason.) Create an instance of ColorConverter instead of calling it statically; see my answer. – Tom Galvin Dec 04 '14 at 20:50
  • @Quackmatic i really dont get it, when i use breakpoints my array is filled with the hexadecimal colors, but still it gives me formatexception – Niels Bril Dec 04 '14 at 20:59
  • Can you give the actual message of the exception? Preferably translated into English if it isn't already. – Tom Galvin Dec 04 '14 at 21:00
  • @Quackmatic here is the project: https://www.dropbox.com/s/tfv7r81gcnwv284/8Oefening3.rar?dl=0 sorry for bothering you – Niels Bril Dec 04 '14 at 21:03
  • That doesn't help. I can't help you unless you give me the exact message of the Exception being thrown. – Tom Galvin Dec 04 '14 at 21:06
  • @Quackmatic thanks already: https://www.dropbox.com/s/0dbvbejoa51sar0/formatexception.PNG?dl=0 – Niels Bril Dec 04 '14 at 21:12
  • What is the exact content of `sKleur` when the exception is thrown? – Tom Galvin Dec 04 '14 at 21:16
  • string sKleur = sLine; and sLine is read 6 times (Black, Green, etc). Kleur is Dutch for color ;) – Niels Bril Dec 04 '14 at 21:17
  • Try replacing `sKleur` with `sKleur.Trim()` in the call to `ConvertFromInvariantString`. There may be leading or trailing whitespace in the string. – Tom Galvin Dec 04 '14 at 21:19
  • @Quackmatic still doesnt work, but dont bother about it anymore, i will ask my teacher next week. thanks for your time! – Niels Bril Dec 04 '14 at 21:22
0

Try this:

var color = (Color)ColorConverter.ConvertFromString(sLine);
var brush = new SolidColorBrush(color);
Pavel Pykhtin
  • 353
  • 3
  • 11