【问题标题】:Saving list of Bitmap Images位图图像保存列表
【发布时间】:2017-02-16 21:16:48
【问题描述】:

我有一个位图图像列表。我需要将它们保存到本地文件夹。 这不适用于 Windows 10 通用应用程序。

var serializer = new DataContractSerializer(typeof(List<BitmapImage>));
    using (var stream = await ApplicationData.Current.LocalCacheFolder.OpenStreamForWriteAsync(fileName, CreationCollisionOption.ReplaceExisting)) {
            serializer.WriteObject(stream, collection);
        }

WriteObject 方法抛出以下错误

Exception thrown: 'System.Runtime.Serialization.InvalidDataContractException' in System.Private.DataContractSerialization.dll

【问题讨论】:

  • “这不起作用” - 你遇到什么错误?
  • 请查看已编辑的问题。谢谢

标签: c# uwp file-storage


【解决方案1】:

BitmapImage 不可序列化。将其转换为字节数组并将其写入磁盘:

public static byte[] ConvertToBytes(BitmapImage bitmapImage)
{
    using (var ms = new MemoryStream())
    {
        var btmMap = new WriteableBitmap(bitmapImage.PixelWidth, bitmapImage.PixelHeight);
        btmMap.SaveJpeg(ms, bitmapImage.PixelWidth, bitmapImage.PixelHeight, 0, 100);
        return ms.ToArray();
    }
}

var serializer = new DataContractSerializer(typeof(byte[]));
    using (var stream = await ApplicationData.Current.LocalCacheFolder.OpenStreamForWriteAsync(fileName, CreationCollisionOption.ReplaceExisting)) {
        serializer.WriteObject(stream, ConvertToBytes(collection));
    }

【讨论】:

  • 如何将字节转换回位图图像?
【解决方案2】:

您无法从BitmapImage 中提取位图。无法将BitmapImage 直接保存到文件中。唯一的方法是记住原始来源并将其保存。有关将BitmapImage 保存到文件的更多详细信息,请参考this thread

如果您知道原始来源,例如您从FileOpenPicker选取的文件中读取BitmapImage,那么您可以将图像文件读取到WriteableBitmap然后您可以提取PixelBuffer,使用BitmapEncoder 对其进行编码,然后将生成的流保存到StorageFile,如 Rob 所说。示例代码如下:

private async void btncreate_Click(object sender, RoutedEventArgs e)
{
    FileOpenPicker openpicker = new FileOpenPicker();
    openpicker.FileTypeFilter.Add(".jpg");
    openpicker.FileTypeFilter.Add(".png");
    StorageFile originalimage = await openpicker.PickSingleFileAsync();
    WriteableBitmap writeableimage1;
    using (IRandomAccessStream stream = await originalimage.OpenAsync(FileAccessMode.Read))
    {
        SoftwareBitmap softwareBitmap;
        BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
        softwareBitmap = await decoder.GetSoftwareBitmapAsync();
        writeableimage1 = new WriteableBitmap(softwareBitmap.PixelWidth, softwareBitmap.PixelHeight);
        writeableimage1.SetSource(stream);
    }
    StorageFolder folder = ApplicationData.Current.LocalFolder;
    StorageFile newimage = await folder.CreateFileAsync(originalimage.Name, CreationCollisionOption.ReplaceExisting);
    using (IRandomAccessStream ras = await newimage.OpenAsync(FileAccessMode.ReadWrite))
    {
        BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, ras);
        var stream = writeableimage1.PixelBuffer.AsStream();
        byte[] buffer = new byte[stream.Length];
        await stream.ReadAsync(buffer, 0, buffer.Length);
        encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)writeableimage1.PixelWidth, (uint)writeableimage1.PixelHeight, 96.0, 96.0, buffer);
        await encoder.FlushAsync();
    }
}

图片列表,可能需要一张一张保存。

【讨论】:

    猜你喜欢
    • 2020-09-19
    • 2016-05-19
    • 2014-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-17
    • 2011-05-08
    • 2017-02-05
    相关资源
    最近更新 更多