如何直接从zipfile加载图像源。
我使用DotNetZip。
以下2个代码示例。
代码示例1无法加载并显示图像文件。
代码示例1 我将一个zipEntry加载到MemoryStream,并创建一个BitmapImage用作Imagesource但它失败了。??? 只是为了测试我尝试从内存流中保存图像,以确保加载Zip条目。它保存正确:
MemoryStream memoryStream1 = new MemoryStream();
ZipFile zip = ZipFile.Read(@"D:\myZipArcive.zip");
ZipEntry myEntry = zip["myfile.jpg"];
myEntry.Extract(memoryStream1);
memoryStream1.Close();
System.IO.File.WriteAllBytes(@"D:\saved.jpg", memoryStream1.ToArray());
//save file just for testing
BitmapImage src = new BitmapImage();
src.BeginInit();
src.CacheOption = BitmapCacheOption.OnLoad;
src.StreamSource = memoryStream1;
src.EndInit();
myImage.Source = src as ImageSource; //no image is displayed
代码示例2 我将一个文件加载到MemoryStream,并创建一个BitmapImage,用作带有成功的Imagesource。
FileStream fileStream = File.OpenRead(@"D:\myFile.jpg");
MemoryStream memoryStream2 = new MemoryStream();
memoryStream2.SetLength(fileStream.Length);
fileStream.Read(memoryStream2.GetBuffer(), 0, (int)fileStream.Length);
BitmapImage src = new BitmapImage();
src.BeginInit();
src.CacheOption = BitmapCacheOption.OnLoad;
src.StreamSource = memoryStream2;
src.EndInit();
myImage.Source = src as ImageSource; //The image is displayed
示例1中的MemoryStream有什么问题 当我执行示例1时,我收到一条调试消息: “在mscorlib.dll中发生'System.NotSupportedException'类型的第一次机会异常”
答案 0 :(得分:1)
Stream似乎被Extract方法关闭了。但是,您可以从第一个缓冲区创建第二个MemoryStream:
var zipFile = ZipFile.Read(@"D:\myZipArcive.zip");
var zipEntry = zipFile["myfile.jpg"];
var zipStream = new MemoryStream();
zipEntry.Extract(zipStream);
var bitmap = new BitmapImage();
using (var sourceStream = new MemoryStream(zipStream.ToArray()))
{
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.StreamSource = sourceStream;
bitmap.EndInit();
}
myImage.Source = bitmap;
更新:因为您事先知道图像缓冲区的未压缩大小,所以可以通过手动提供两个MemoryStream操作的缓冲区来略微提高性能:
var buffer = new byte[zipEntry.UncompressedSize];
var zipStream = new MemoryStream(buffer); // here
zipEntry.Extract(zipStream);
...
using (var sourceStream = new MemoryStream(buffer)) // and here
...
答案 1 :(得分:0)
这也有效:
var zipFile = ZipFile.Read(@"D:\myZipArcive.zip");
var zipEntry = zipFile["myfile.jpg"];
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.StreamSource = zipEntry.OpenReader();
bitmap.EndInit();
myImage.Source = bitmap;