在 WinRT 中序列化 BitmapImage [英] Serializing a BitmapImage in WinRT

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

问题描述

public static async Task SaveFileAsync(string FileName, T data)
{
    MemoryStream memStream = new MemoryStream();
    DataContractSerializer serializer = new DataContractSerializer(typeof(T));
    serializer.WriteObject(memStream, data);

    StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(FileName,
        CreationCollisionOption.ReplaceExisting);
    using (Stream stream = await file.OpenStreamForWriteAsync())
    {
        memStream.Seek(0, SeekOrigin.Begin);
        await memStream.CopyToAsync(stream);
        await stream.FlushAsync();
    }
}

public static async Task<T> RestoreFileAsync(string FileName)
{
    T result = default(T);
    try
    {
        StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(FileName);
        using (IInputStream inStream = await file.OpenSequentialReadAsync())
        {
            DataContractSerializer serializer = new DataContractSerializer(typeof(T));
            result = (T)serializer.ReadObject(inStream.AsStreamForRead());
            return result;
        }
    }

    catch (FileNotFoundException)
    {
        return default(T);
    }
}

我正在使用这些方法来序列化我的数据,但我有一个包含图像的类,

I'm using these methods to serialize my data, but i have a class that contains an image,

[DataMember]
Public Image img{get;set;}

我正在尝试对其进行序列化.我实际上正在执行以下操作

I'm trying to serialize it. I'm doing the following actually

var thumb = await item.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.PicturesView,
                        1000, Windows.Storage.FileProperties.ThumbnailOptions.UseCurrentScale);

BitmapImage bmg = new BitmapImage();
bmg.SetSource(thumb);
Image img = new Image();
img.Source = bmg;

我尝试自行序列化 bitmapImage,但这是同样的问题.我不断收到此错误,并且我的 BitmapImage 有一个属性.

i tried to serialize the bitmapImage it self but it is the same problem. i keep getting this error, and my BitmapImage has an attribute.

无法序列化类型Windows.UI.Xaml.Media.ImageSource".考虑使用 DataContractAttribute 属性对其进行标记,并使用 DataMemberAttribute 属性标记您想要序列化的所有成员.如果该类型是一个集合,请考虑使用 CollectionDataContractAttribute 对其进行标记.有关其他支持的类型,请参阅 Microsoft .NET Framework 文档.

Type 'Windows.UI.Xaml.Media.ImageSource' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.

推荐答案

DataContractSerializer 不适用于图像.您应该使用 BitmapEncoder(如果您正在处理 WriteableBitmap 或只是序列化 BitmapImage 的源地址.如果位图是从本地路径或临时加载的URL 并且您想要保留整个位图 - 您无法从 BitmapImage 中提取位图位,因此您需要从原始源 URL 下载源文件或复制您加载的本地文件. 然后,您可以将该副本保存为松散文件,或者在 DataContractSerializer-created XML 中序列化为 Base64.

DataContractSerializer won't work with images. You should either use BitmapEncoder (if you are dealing with WriteableBitmap or simply serialize the source address of your BitmapImage. If the bitmap was loaded from a local path or a temporary URL and you want to persist the entire bitmap - you can't extract the bitmap bits from BitmapImage anyway and so you need to either download the source file from the original source URL or copy the local file you loaded. You could then either save that copy as a loose file or serialize as say Base64 inside of your DataContractSerializer-created XML.

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

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