保存位图图像列表

时间:2017-02-16 21:16:48

标签: c# uwp file-storage

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

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

2 个答案:

答案 0 :(得分: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));
    }

答案 1 :(得分:0)

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

如果您知道原始来源,例如,您从BitmapImage选择的文件中读取FileOpenPicker,那么您可以将图像文件读取到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();
    }
}

对于图像列表,您可能需要逐个保存。