gpt4 book ai didi

c# - 不支持位图编码器保存

转载 作者:行者123 更新时间:2023-12-03 23:21:49 24 4
gpt4 key购买 nike

我有以下代码,我看不出有什么问题,关于问题可能是什么的任何想法?

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 occurred HResult=-2146233067

Message=Specified method is not supported. Source=PresentationCore

StackTrace: at System.Windows.Media.Imaging.BitmapEncoder.Save(Stream stream) at FileFormatters.Export.SaveImage(BitmapFrame data, Stream saveStream)

InnerException: null



MSDN页面说

NotSupportedException :The Frames value that is passed to the encoder is null.

NotSupportedException :The Frames count is less than or equal to zero.



但是帧数为 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"

最佳答案

似乎有必要将图像缓冲区写入中间 MemoryStream,然后才能将其写入 ZipEntry Stream:

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);
}
}

关于c# - 不支持位图编码器保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38972422/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com