-2

Edit: the map[][] is filled with buttons

The main issue is that an .image seems not to be able to save . i want to make a load that when i load it in every map[i][j] regains its previous state. everything seems to work exept for the .image

FileStream file = new FileStream(@""+AppDomain.CurrentDomain.BaseDirectory+ "\\objects\\savegame"+spacing+".sav", FileMode.Create, FileAccess.ReadWrite);

StreamWriter sw = new StreamWriter(file);
        for (Int32 i = 0; i <columns; i++)
        {
            for (Int32 j = 0; j < columns; j++)
            {
                sw.WriteLine(map[i][j].Enabled);
                sw.WriteLine(map[i][j].Enabled);
                sw.WriteLine(map[i][j].Image);
                sw.WriteLine(map[i][j].Tag);
                sw.WriteLine(map[i][j].Text);
                sw.WriteLine(map[i][j].Name);
                sw.WriteLine( map[i][j].Height);
                sw.WriteLine(map[i][j].Width);
            }
        } 


FileStream file = new FileStream(@"" + AppDomain.CurrentDomain.BaseDirectory + "\\objects\\savegame" + spacing + ".sav", FileMode.Create, FileAccess.ReadWrite);
    StreamReader sr = new StreamReader(file);  
for (Int32 i = 0; i < columns; i++)
    {
        for (Int32 j = 0; j < columns; j++)
        {
            map[i][j].Enabled = Convert.ToBoolean(sr.ReadLine());
            map[i][j].Image = Convert.ToString(sr.ReadLine());// this is the problem
            map[i][j].Tag = Convert.ToString(sr.ReadLine());
            map[i][j].Text = Convert.ToString(sr.ReadLine());
            map[i][j].Name = Convert.ToString(sr.ReadLine());
            map[i][j].Height = Convert.ToInt32(sr.ReadLine());
            map[i][j].Width = Convert.ToInt32(sr.ReadLine());
        }
    }
sr.Close();
}

//Sample location for the image --->map[i][j].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "objects\\map\\mapgrass" + spacing + ".png");
Kapein
  • 175
  • 13

1 Answers1

1

I don't understand much your question, but I can see one error: Please don't forget to Close your stream after writing, and put your code into using block.

using(StreamWriter sw = new StreamWriter(file))
{
    for (Int32 i = 0; i <columns; i++)
    {
    .....
    }

    sw.Close();
}

Update:

If you use WriteLine(map[i][j].Image), the system actually calls WriteLine(map[i][j].Image.ToString()) which returns name of the Image class. If you want to save any useful information, you must put there map[i][j].Image.something or map[i][j].whatever_useful. Image is a binary object - usually a bitmap picture, you cannot save it to a text file as a whole and load it back from it.

Al Kepp
  • 5,831
  • 2
  • 28
  • 48
  • oh well, i forgot to add the sw.Close in the post, Error 4 Cannot implicitly convert type 'string' to 'System.Drawing.Image' – Kapein Mar 22 '13 at 15:20
  • This was something really good thanks i'll see what i can do now – Kapein Mar 22 '13 at 15:27
  • Please don't tell us you really think that a string can be converted to an Image this way. – Al Kepp Mar 22 '13 at 15:27
  • thought it would set the location something like this : Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "objects\\map\\mapgrass" + spacing + ".png"); but then ill just make an if else setup to do it – Kapein Mar 22 '13 at 15:29