【发布时间】:2014-04-15 13:01:54
【问题描述】:
我使用的是BinaryFormatter,我序列化了一个树视图。
现在我想为ImageList做同样的事情
我用这段代码序列化了:
public static void SerializeImageList(ImageList imglist)
{
FileStream fs = new FileStream("imagelist.iml", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, imglist.ImageStream);
fs.Close();
}
还有这个要反序列化:
public static void DeSerializeImageList(ref ImageList imgList)
{
FileStream fs = new FileStream("imagelist.iml", FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
imgList.ImageStream = (ImageListStreamer)bf.Deserialize(fs);
fs.Close();
}
但我在所有键中都得到一个空字符串!
ImgList.Images.Keys
为什么?
【问题讨论】:
-
因为ImageListStreamer只序列化ImageList数据,不能替代序列化一个完整的ImageList。您正在以不打算使用的方式使用它,它是一个帮助类,用于将 ImageList 数据序列化为 .resx 文件。设计师使用它。
-
@HansPassant 我使用的 ImageList 可以在运行时更改,我在 TreeView 中使用它的键来显示图像(附加后),您建议将其保存在哪里?将其保存在 .resx 文件中?如果是这样,如何?或者例如将键保存在文本文件中并在表单加载中填充 ImageList 键?
标签: c# serialization