【问题标题】:Serializing a BitmapImage in WinRT在 WinRT 中序列化 BitmapImage
【发布时间】:2013-02-21 18:31:12
【问题描述】:
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);
    }
}

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

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

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

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 有一个属性。

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

【问题讨论】:

    标签: c# serialization windows-8 windows-runtime winrt-xaml


    【解决方案1】:

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

    【讨论】:

    • Filip,如您所见,我从视频中以缩略图的形式获取图像,问题是我没有要复制到本地文件夹并将其检索为 Uri 的名称。正如您在上面的代码中看到的,我需要保存位图图像或它的源,我不知道要同时执行这两个操作。如果你能提供一个简单的例子,那就太好了,谢谢。
    • 没有简单的例子。您不能序列化 BitmapImage 本身,句号。如果它是使用 Uri 加载的 - 您可以保存 Uri 或使用它来加载原始图像的字节。如果它是用流加载的 - 您需要跟踪该流的来源,并且当您需要序列化 ​​BitmapImage 时 - 您需要序列化该(已经序列化的)流。顺便说一句,如果您的视频缩略图问题得到解决,您可以接受答案吗?如果您找到另一个答案,可以添加您自己的答案吗?
    • 如果你的意思是如何从视频中获取缩略图,代码在上面,var thumb = item.GetThumbnailAsync(...),其中 item 是一个 StorageFile,对不起,但我是英语不是很好。 :D
    • 最后一件事,我找到了这段代码。 var stm = thumb.AsStream(); byte[] bytes = new byte[Convert.ToUInt32(thumb.Size)]; stm.位置 = 0;等待 stm.ReadAsync(bytes, 0, bytes.Length); InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream(); DataWriter writer = new DataWriter(randomAccessStream.GetOutputStreamAt(0)); writer.WriteBytes(字节);等待 writer.StoreAsync(); BitmapImage imagen = new BitmapImage(); imagen.SetSource(randomAccessStream);这可能行得通吗?
    • 这似乎大致类似于您可以将StorageItemThumbnail 转换为BitmapImage 的操作,尽管感觉可能有点过多,因为它看起来像是将流复制到缓冲区,然后在从中加载BitmapImage 之前写入另一个流,但你说你想序列化它。是否要序列化包含压缩图像字节的 randomAccessStream?
    猜你喜欢
    • 2011-07-22
    • 1970-01-01
    • 2014-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多