方法序列化包含的BitmapImage类:使用2继承的类? [英] Method to serialize a class which contain BitmapImage : using 2 inherited class?

查看:444
本文介绍了方法序列化包含的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.

我要在我的类的对象,它可以用来对一个图像源结合,并且能够序列化这个类。

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.

但是,如果你必须序列化的位图,只需保存位图。以一个MemoryStream以byte []

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);
        }
    }



更新:



Update:

[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对象,具有选择加入的属性,这意味着你需要指定哪些属性进行序列化。

这个演示对象都有一个名称属性,序列化,一个的ImagePath属性,序列化。< BR>
然而,图像属性不是序列化。

当反序列化对象的图像将加载,因为的ImagePath setter方法​​所需的funcionality。



我希望这有助于,我测试了它,它作品。

率,如果你喜欢。祝你好运!

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继承的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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