English 中文(简体)
Load image into memory immediately
原标题:

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?

最佳回答

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.

问题回答

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




相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签