Serializing a BitmapImage in WinRT(在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 WinRT 中序列化 BitmapImage
基础教程推荐
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 XML 转换为通用列表 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01