Load image into memory immediately

2019-01-28 17:40发布

问题:

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?

回答1:

Use CacheOption = BitmapCacheOption.OnLoad

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.

See also my answer to this question

Update

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

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.



回答2:

I figured it out. I have to use MemoryStream:

MemoryStream ms = new MemoryStream(File.ReadAllBytes(image));
TiffBitmapDecoder decoder = new TiffBitmapDecoder(ms, BitmapCreateOptions.None, BitmapCacheOption.None);
List<BitmapFrame> images = new List<BitmapFrame>();
foreach (BitmapFrame frame in decoder.Frames) images.Add(frame);