1

I am not trying to convert brush to string, but brush array to string.

I need to write a txt file on xml and then I am trying to convert a brush[] into a string, but I do not know how to this directly.

So, I tried to convert first to color and then I would convert color to string.

I am struggling with the first conversion where the code is not allowing me to write this part:

Color[] cor1_local = new Pen(cor_local[i]).Color[];

cor_local is brush[].

Code:

xml.WriteStartElement("cor_frmlocal");
for (int i = 0; i < cor_local.Length; i++)
{
    Color[] cor1_local = new Pen(cor_local[i]).Color[];
    xml.WriteElementString("Cor_local", cor1_local[i].ToArgb().ToString());
}
xml.WriteEndElement();
motipai
  • 328
  • 3
  • 10
  • 1
    I would suggest [*serializing*](https://msdn.microsoft.com/en-us/library/182eeyhh%28v=vs.110%29.aspx) the colors and brushes. – cbr Oct 15 '15 at 18:19

1 Answers1

1

Try to use that

        xml.WriteStartElement("cor_frmlocal");
        for (int i = 0; i < cor_local.Length; i++)
        {
            xml.WriteElementString("Cor_local", ((SolidBrush)cor_local[i]).Color.ToArgb().ToString());
        }
        xml.WriteEndElement();

the problem was in usage of base Brush class object references that do not contain Color property. To handle that you must cast it back to SolidBrush.

To convert brush back

1) create the list of brushes var brushList = new List<Brush>();

2) read color value as var colorValue = Convert.ToInt32(reader.ReadElementString());

3) create color from that value var color = Color.FromArgb(colorValue);

4) create new brush and add it to list list.Add(new SolidBrush(color));

the result code looks like

XmlTextReader reader = new XmlTextReader(filename);
string node_info = "";
var brushList = new List<Brush>();

while(reader.Read())
{
    node_info = reader.Name;

    if (node_info == "cor_frmlocal")
    {
         var colorValue = Convert.ToInt32(reader.ReadElementString());
         var color = Color.FromArgb(colorValue);
         list.Add(new SolidBrush(color));
    }
}
Mykola
  • 3,343
  • 6
  • 23
  • 39
  • I have tried to use it before, but it gets problem on the .Color – motipai Oct 15 '15 at 18:24
  • `ToArgb() ` method returns unsigned int value – Mykola Oct 15 '15 at 18:25
  • 'System.Drawing.Brush' does not contain a definition for 'Color' and no extension method 'Color' accepting a first argument of type 'System.Drawing.Brush' could be found (are you missing a using directive or an assembly reference?) – motipai Oct 15 '15 at 18:28
  • What kind of brush you have used? – Mykola Oct 15 '15 at 18:32
  • If you using regular brush, it must work now. – Mykola Oct 15 '15 at 18:35
  • I am using solidbrush. I also tried: xml.WriteStartElement("cor_frmlocal"); for (int i = 0; i < cor_local.Length; i++) { Color cor1_local = new Pen(cor_local[i]).Color; cor2_local[i] = cor1_local; xml.WriteElementString("Cor_local", cor2_local[i].ToArgb().ToString()); } xml.WriteEndElement(); I got no errors back but while running, it stops on: cor2_local[i] = cor1_local; – motipai Oct 15 '15 at 18:40
  • Watch up to the code it must work now. – Mykola Oct 15 '15 at 18:42
  • it worked fine! Thanks!! and what about the way back? I changed my code about how I am trying to convert from string to brush[]. – motipai Oct 15 '15 at 19:01
  • do not forgot to vote + :) – Mykola Oct 15 '15 at 20:15