立即将图像加载到内存中 [英] Load image into memory immediately

查看:111
本文介绍了立即将图像加载到内存中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将WPF中Tiff图像的所有帧打开到内存中,然后删除源.之后,我最终需要渲染该图像(根据窗口大小调整大小).我的解决方案速度很慢,无法在第一个要求之前删除文件源.有最佳做法吗?

I need to open all frames from Tiff image in WPF into memory and then delete the source. And after that I eventually need to render that image (resized according to window size). My solution is quite slow and I cannot delete file source before the first require. Any best practices?

推荐答案

使用CacheOption = BitmapCacheOption.OnLoad

此选项可与BitmapImage.CacheOption属性一起使用,或用作BitmapDecoder.Create()的参数.如果要在加载图像后访问多个帧,则必须使用BitmapDecoder.Create.无论哪种情况,文件都将完全加载并关闭.

This option can be used with the BitmapImage.CacheOption property or as an argument to BitmapDecoder.Create() If you want to access multiple frames once the images is loaded you'll have to use BitmapDecoder.Create. In either case the file will be loaded fully and closed.

另请参阅我对此问题的答案

更新

以下代码非常适合加载图像的所有帧并删除文件:

The following code works perfectly for loading in all the frames of an image and deleting the file:

var decoder = BitmapDecoder.Create(new Uri(imageFileName), BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
List<BitmapFrame> images = decoder.Frames.ToList();
File.Delete(imageFileName);

当然,您也可以访问解码器.删除文件后会显示帧.

You can also access decoder.Frames after the file is deleted, of course.

如果您想自己打开视频流,此变体也可以使用:

This variant also works if you prefer to open the stream yourself:

List<BitmapFrame> images;
using(var stream = File.OpenRead(imageFileName))
{
  var decoder = BitmapDecoder.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
  images = decoder.Frames.ToList();
}
File.Delete(imageFileName);

无论哪种情况,它都比创建MemoryStream更有效,因为MemoryStream一次将两个数据副本保留在内存中:解码副本和未解码副本.

In either case it is more efficient than creating a MemoryStream because a MemoryStream keeps two copies of the data in memory at once: The decoded copy and the undecoded copy.

这篇关于立即将图像加载到内存中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆