Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 不支持BitmapEncoder保存_C#_Wpf_Ziparchive_Bitmapencoder - Fatal编程技术网

C# 不支持BitmapEncoder保存

C# 不支持BitmapEncoder保存,c#,wpf,ziparchive,bitmapencoder,C#,Wpf,Ziparchive,Bitmapencoder,我有下面的代码,我看不出任何错误,对问题可能是什么有什么想法 private static string SaveBaseImage( ZipArchive arc, DBImage image, int imageIndex ) { using (var mem = new MemoryStream(image.Data)) { var bmp = BitmapFrame.Create(mem); //var bmp = BitmapFrame.

我有下面的代码,我看不出任何错误,对问题可能是什么有什么想法

private static string SaveBaseImage( ZipArchive arc, DBImage image, int imageIndex )
{
    using (var mem = new MemoryStream(image.Data))
    {
        var bmp = BitmapFrame.Create(mem);
        //var bmp = BitmapFrame.Create(m‌​em, BitmapCreateOptions.‌​None, BitmapCacheOption.On‌​Load);
        var codex = bmp.Decoder.CodecInfo;

        var filename = $"{imageIndex}{codex.FileExtensions}";
        var imagezip = arc.CreateEntry(filename,CompressionLevel.Optimal));
        using (var imagestream = imagezip.Open())
        {
            SaveImage( bmp, imagestream);
        }
        return filename;
    }
}

private static void SaveImage(BitmapFrame data, Stream saveStream)
{
    var codex = data.Decoder.CodecInfo;
    var encoder = BitmapEncoder.Create(codex.ContainerFormat);
    encoder.Frames.Add(data);
    encoder.Save(saveStream);
}
当我跑的时候,它会扔

发生System.NotSupportedException HResult=-2146233067

Message=不支持指定的方法。Source=PresentationCore

堆栈跟踪: 位于System.Windows.Media.Imaging.BitmapEncoder.Save(流) 在FileFormatters.Export.SaveImage(位图帧数据,流saveStream)

InnerException:null

MSDN页面上说

NotSupportedException:传递给编码器的帧值为空

NotSupportedException:帧计数小于或等于零

但是,帧数为1,数据不为空

进一步资料

arc declared as using (ZipArchive arc = new ZipArchive(stream, ZipArchiveMode.Create))
image.Data is byte[]
codex.FriendlyName = "PNG Decoder"
encoder.CodecInfo.FriendlyName = "PNG Encoder"

在将图像缓冲区写入ZipEntry流之前,似乎有必要将其写入中间内存流:

private static void SaveImage(BitmapFrame data, Stream saveStream)
{
    var encoder = BitmapEncoder.Create(data.Decoder.CodecInfo.ContainerFormat);
    encoder.Frames.Add(data);

    using (var memoryStream = new MemoryStream())
    {
        encoder.Save(memoryStream);
        memoryStream.Position = 0;
        memoryStream.CopyTo(saveStream);
    }
}

通过
var bmp=BitmapFrame.Create(mem,BitmapCreateOptions.None,BitmapCacheOption.OnLoad),尝试立即从MemoryStream加载位图
除此之外,你不能把原始的
图像.数据
写入档案吗?@Clemens谢谢你的帮助,不幸的是,我想到的第一件事毫无效果,至于跳过解码恐怕不行,因为下一个操作是在帧顶部绘制笔划集合并将其保存为第二个图像,所以位图编码器不支持压缩流?这将是在MSDN页面中包含的有用信息