序列化在WinRT的一个BitmapImage的 [英] Serializing a BitmapImage in WinRT
问题描述
公共静态异步任务SaveFileAsync(字符串文件名,T数据)
{
MemoryStream的memStream =新的MemoryStream();
DataContractSerializer的序列化=新的DataContractSerializer(typeof运算(T));
serializer.WriteObject(memStream,数据);
StorageFile文件=等待ApplicationData.Current.LocalFolder.CreateFileAsync(文件名,
CreationCollisionOption.ReplaceExisting);使用
(流流=等待file.OpenStreamForWriteAsync())
{
memStream.Seek(0,SeekOrigin.Begin);
等待memStream.CopyToAsync(流);
等待stream.FlushAsync();
}
}
公共静态异步任务< T> RestoreFileAsync(字符串文件名)
{$ B $(B T)结果=默认(T);
试
{
StorageFile文件=等待ApplicationData.Current.LocalFolder.GetFileAsync(文件名);使用
(IInputStream inStream中等待= file.OpenSequentialReadAsync())
{
DataContractSerializer的序列化=新的DataContractSerializer(typeof运算(T));
结果=(T)serializer.ReadObject(inStream.AsStreamForRead());
返回结果;
}
}
赶上(FileNotFoundException异常)
{
返回默认值(T);
}
}
我用这些方法来序列化我的数据,
,但我有一个包含一个图像类,
[数据成员]
公众形象IMG {得到;设置;}
我想序列化。
我做下面的实际
VAR拇指=等待item.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode .PicturesView,
1000,Windows.Storage.FileProperties.ThumbnailOptions.UseCurrentScale);
的BitmapImage BMG =新的BitmapImage();
bmg.SetSource(大拇指);
图片IMG =新的图像();
img.Source = BMG;
我试图序列化的BitmapImage它自身却是同样的问题。
I不断收到这个错误,我的BitmapImage的有一个属性。
键入Windows.UI.Xaml.Media.ImageSource不能被序列化。考虑与DataContractAttribute属性标记它,标志着其所有成员的你想序列化与DataMemberAttribute属性。如果该类型是一个集合,考虑与CollectionDataContractAttribute标记它。看到其他支持的类型的Microsoft .NET Framework文档。
块引用>
解决方案
的DataContractSerializer
将不会与影像工作。您应该使用的BitmapEncoder
(如果你正在处理WriteableBitmap的
或系列化你的源地址的BitmapImage
如果位图是从本地路径或临时网址加载,并且要持续整个位 - 你无法从的BitmapImage
反正所以你需要从原始来源网址下载源文件或复制您加载本地文件。然后,您可以要么副本保存为一个松散的文件或序列化为说Base64编码的内部你的DataContractSerializer的
-created XML。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.
解决方案
DataContractSerializer
won't work with images. You should either useBitmapEncoder
(if you are dealing withWriteableBitmap
or simply serialize the source address of yourBitmapImage
. 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 fromBitmapImage
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 yourDataContractSerializer
-created XML.这篇关于序列化在WinRT的一个BitmapImage的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!