本文介绍了序列化包含 BitmapImage 的类的方法:使用 2 个继承的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的应用程序关闭时,我想序列化一些数据以使其持久化以供下次应用程序使用.我选择使用 Newtonsoft.JsonConverter 序列化这些数据.但是,我的班级中有一个 BitmapImage,它不能被序列化.

When my application closes, I want to serialize some data to make it persistent for the next use of the application. I choose to serialize these data with Newtonsoft.JsonConverter.But, I have a BitmapImage in my class, and it can't be serialized.

我有点坚持这一点,因为我没有找到将我的 BitmapImage 保留在我的班级中(我需要将它保留在这里)并能够序列化这个班级的解决方案.我试图创建一个包含 BitmapImage 的继承类,但不允许从我的基类创建隐式运算符.

I'm bit stuck on this because I don't find a solution to keep my BitmapImage into my class (I need to keep it here) and being able to serialize this class.I tried to create a inherited class which contains the BitmapImage, but I'm not allowed to create an implicit operator from my base class.

我想在我的类中有一个对象,它可以用作 Image 绑定的源,并且能够序列化这个类.

I want to have an object in my class which can be used to be a source for a Image binding, and be able to serialize this class.

推荐答案

我建议只将位图保存到文件"并仅序列化图像文件名.

I would suggest just "Saving the BitMap to a file" and serializing the image file name only.

但是,如果您必须序列化位图,只需将位图以 byte[] 的形式保存到 MemoryStream 中.

But, if you must serialize the bitmap, just save the bitmap to a MemoryStream as byte[].

byte[] byteArray;

using (MemoryStream stream = new MemoryStream())
{
        Image.Save(stream, ImageFormat.Bmp);
        byteArray = stream.ToArray();
}

更新:

通过图像路径序列化您的图像.

Update:

Serialize your image via Image Path.

private string m_imagePath;

    public Image Image { get; private set; }

    public string ImagePath
    {
        get { return m_imagePath; }
        set
        {
            m_imagePath = value;
            Image = Image.FromFile(m_imagePath);
        }
    }

更新:

[JsonObject(MemberSerialization.OptIn)]
public class MyClass
{
    private string m_imagePath;

    [JsonProperty]
    public string Name { get; set; }

    // not serialized because mode is opt-in
    public Image Image { get; private set; }

    [JsonProperty]
    public string ImagePath
    {
        get { return m_imagePath; }
        set
        {
            m_imagePath = value;
            Image = Image.FromFile(m_imagePath);
        }
    }
}

如您所见,这里有一个 json 对象,它具有 Opt-In 属性,这意味着您需要指定要序列化的属性.
这个演示对象有一个被序列化的 Name 属性,一个被序列化的 ImagePath 属性.
但是,Image 属性未序列化.
当您反序列化对象时,图像将加载,因为 ImagePath 设置器具有所需的功能.

我希望这会有所帮助,我对其进行了测试,并且可以正常工作.
喜欢就评价.祝你好运!

As you can see, you have a json object here, that has the Opt-In attribute, meaning you need to specify which properties are serialized.
This demo object has a Name property that is serialized, an ImagePath property that is serialized.
However, the Image property is not serialized.
When you deserialize the object the image will load because the ImagePath setter has the needed funcionality.

I hope this helped, I tested it, and it works.
Rate if you like. Good Luck!

这篇关于序列化包含 BitmapImage 的类的方法:使用 2 个继承的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 22:55