如何使用带有button.Image的流阅读器

本文关键字:Image 何使用 button | 更新日期: 2023-09-27 18:05:14

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

主要问题是。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");

如何使用带有button.Image的流阅读器

我不太明白你的问题,但我可以看到一个错误:请不要忘记写完后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())返回图像类的名称。如果你想保存任何有用的信息,你必须把map[i][j].Image.somethingmap[i][j].whatever_useful放在那里。图像是二进制对象-通常是位图图片,您不能将其作为一个整体保存到文本文件中并从中加载它。