首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >保存位图图像列表

保存位图图像列表
EN

Stack Overflow用户
提问于 2017-02-17 05:16:48
回答 2查看 1.1K关注 0票数 1

我有一个位图图像列表。我需要将它们保存到本地文件夹。这在Windows10通用应用程序上不起作用。

代码语言:javascript
复制
var serializer = new DataContractSerializer(typeof(List<BitmapImage>));
    using (var stream = await ApplicationData.Current.LocalCacheFolder.OpenStreamForWriteAsync(fileName, CreationCollisionOption.ReplaceExisting)) {
            serializer.WriteObject(stream, collection);
        }

WriteObject方法引发以下错误

代码语言:javascript
复制
Exception thrown: 'System.Runtime.Serialization.InvalidDataContractException' in System.Private.DataContractSerialization.dll
EN

回答 2

Stack Overflow用户

发布于 2017-02-17 05:33:49

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

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2017-03-01 10:21:11

不能从BitmapImage中提取位图。没有办法直接将BitmapImage保存到文件中。唯一的办法就是记住原始源,然后保存下来。有关将BitmapImage保存到文件的更多详细信息,请参考this thread

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

代码语言:javascript
复制
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();
    }
}

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42284802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档