我有以下代码,但我看不出有什么问题,请问可能是什么问题?
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(mem, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
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=指定的方法不受支持。 Source=PresentationCore
StackTrace: at System.Windows.Media.Imaging.BitmapEncoder.Save(Stream stream) at FileFormatters.Export.SaveImage(BitmapFrame data, Stream saveStream)
InnerException: null
MSDN页面显示:
NotSupportedException:传递给编码器的框架值为null。
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"
var bmp = BitmapFrame.Create(mem, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
立即从MemoryStream加载位图。此外,您是否可以将原始的image.Data
写入存档中? - Clemens