如何使用带有button.Image的streamreader

时间:2013-03-22 15:03:30

标签: c# streamreader streamwriter

编辑:地图[] []填充了按钮

主要问题是.image似乎无法保存。 当我在每张地图中加载它时,我想要加载[i] [j]重新恢复其先前的状态。一切似乎都适用于.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");

1 个答案:

答案 0 :(得分:1)

我对您的问题了解不多,但我发现有一个错误:写完后请不要忘记Close您的信息流,并将您的代码放入using阻止。

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

    sw.Close();
}

<强>更新

如果使用WriteLine(map[i][j].Image),系统实际调用WriteLine(map[i][j].Image.ToString()),它返回Image类的名称。如果您想保存所有有用的信息,则必须放在map[i][j].Image.somethingmap[i][j].whatever_useful。图像是二进制对象 - 通常是位图图片,您无法将其整体保存到文本文件中并从中加载。