且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#中:将多幅图片保存到一个文件

更新时间:2023-12-02 19:07:10

Image.FromStream 方法需要一个有效的图像流。你是串连多个图像到一个文件,如果你想重建他们还需要保存它们的大小除了自己的号码。一个更容易的解决办法是简单的二进制序列化图像词典:

The Image.FromStream method expects a valid image stream. You are concatenating multiple images into a single file and if you want to reconstruct them you will also need to save their size in addition to their number. An easier solution would be to simply binary serialize the image dictionary:

public void Save(string filename)
{
    var serializer = new BinaryFormatter();
    using (var stream = File.Create(filename))
    {
        serializer.Serialize(stream, dict);
    }
}

public void Load(string filename)
{
    var serializer = new BinaryFormatter();
    using (var stream = File.Open(filename, FileMode.Open))
    {
        dict = (Dictionary<string, Image>)serializer.Deserialize(stream);
    }
}