Serializing a BitmapImage in WinRT

淺唱寂寞╮ 提交于 2019-12-31 05:30:05

问题


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;

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.

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.


回答1:


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.



来源:https://stackoverflow.com/questions/14997495/serializing-a-bitmapimage-in-winrt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!