在XML文件中序列化和存储图像 [英] Serialize and Store an Image in an XML File

查看:134
本文介绍了在XML文件中序列化和存储图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一点问题。我有一个程序,可以构建一个可观察的用户集合。用户有名字,姓氏和图像。我可以将用户添加到observable集合中,但我也想保存集合并在每次重新打开程序时加载它。

Got a little bit of a problem. I have a program that builds an observable collection of Users. The User has a Firstname, Lastname, and Image. I can add the user to the observable collection, but I also want to save the collection and load it everytime I reopen the program.

我的问题是,虽然保存名字和姓氏相当容易,但编写者无法将图像写入xml文件。有没有办法解决这个问题?

My problem is that while its fairly easy to save a firstname and lastname, the writer can't write the image to the xml file. Is there any way around this?

这是我到目前为止所拥有的:

Here's what I have so far:

可观察的集合:

ObservableCollection<VendorClass> ProfileList = new ObservableCollection<VendorClass>();

有问题的作家:

XmlSerializer xs = new XmlSerializer(typeof(ObservableCollection<VendorClass>));
        using (StreamWriter wr = new StreamWriter("vendors.xml")) //Data/customers.xml
        {
            xs.Serialize(wr, ProfileList);
        }

有什么想法吗?如果确实存在写入图像的解决方案,是否有可行的方法再次读出它?

Any ideas? And if there does exist a solution to write in an image, is there a viable way to read it out again?

推荐答案

XmlSerializer无法序列化或反序列化WPF图像类型,如BitmapImage等。但它能够(反)序列化字节数组。因此,您可以向Person类添加 byte [] ImageBuffer 属性,该类包含二进制图像数据。然后,您还可以在 Image 属性上设置 XmlIgnore 属性以禁止其(反)序列化,并设置<$ ImageBuffer 属性中的c $ c> XmlElement(Image)将其(de)序列化为< Image> ; ...< /图像>

XmlSerializer can't serialize or deserialize the WPF image types like BitmapImage etc. It is however able to (de)serialize byte arrays. So you may add a byte[] ImageBuffer property to your Person class, which contains the binary image data. You would then also set the XmlIgnore attribute on the Image property to suppress its (de)serialization, and set XmlElement("Image") on the ImageBuffer properties to (de)serialize it as <Image>...</Image>.

public class User
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    [XmlIgnore]
    public BitmapSource Image { get; set; }

    [XmlElement("Image")]
    public byte[] ImageBuffer
    {
        get
        {
            byte[] imageBuffer = null;

            if (Image != null)
            {
                using (var stream = new MemoryStream())
                {
                    var encoder = new PngBitmapEncoder(); // or some other encoder
                    encoder.Frames.Add(BitmapFrame.Create(Image));
                    encoder.Save(stream);
                    imageBuffer = stream.ToArray();
                }
            }

            return imageBuffer;
        }
        set
        {
            if (value == null)
            {
                Image = null;
            }
            else
            {
                using (var stream = new MemoryStream(value))
                {
                    var decoder = BitmapDecoder.Create(stream,
                        BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
                    Image = decoder.Frames[0];
                }
            }
        }
    }
}

对于此答案中的Bitmap类型的属性,也建议使用此方法。

This approach has also been suggested for properties of type Bitmap in this answer.

这篇关于在XML文件中序列化和存储图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆