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
- C# 中多线程网络服务器的模式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 输入按键事件处理程序 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01