tags:

views:

309

answers:

3

I am using this C# code to access an image file in order to read metadata from it.

BitmapSource img = BitmapFrame.Create(uri);

Unfortunately the image file specified by uri becomes locked until the program ends. How do I prevent the image from being locked?

A: 

It will be locked for the lifetime of the img object. How long are you keeping it around? Have you tried getting the GC to explicitly dispose of it?

jeffamaphone
img is within the scope of a function that lasts less than a second. Once it is out of scope the file is still locked. There is no BitmapSource.Dispose() . How would I get the GC to explicitly dispose of it?
Liam
+1  A: 

If you want to be able to delete/change the file immediately afterwards, read the whole file into memory, and then give it the MemoryStream instead. For example:

MemoryStream data = new MemoryStream(File.ReadAllBytes(file));
BitmapSource bitmap = BitmapFrame.Create(data);
Jon Skeet
+1  A: 

maybe this could help ?

edit

BitmapSource img = BitmapFrame.Create(uri,BitmapCreateOptions.None,BitmapCacheOption.OnLoad);

BitmapCreateOptions.None = default option

BitmapCacheOption.OnLoad = Caches the entire image into memory at load time. All requests for image data are filled from the memory store.

from here

Fredou