【发布时间】:2014-02-05 15:55:40
【问题描述】:
如何直接从 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,并成功创建了用作 Imagesource 的 BitmapImage。
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' 类型的第一次机会异常"
【问题讨论】:
标签: wpf image load zip memorystream