BitmapImage holding an image file?  
Author Message
Manny06





PostPosted: Windows Presentation Foundation (WPF), BitmapImage holding an image file? Top

I have an application that is loading images in the following manner.

string[] files = Directory.GetFiles("c:\\", "*.jpg");
foreach (string file in files)
{
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(file);
bitmap.CacheOption = BitmapCacheOption.None;
bitmap.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
bitmap.EndInit();

Image image = new Image();
image.Height = 50;
image.Width = 50;
image.Stretch = Stretch.Uniform;
image.Source = bitmap;

panel.Children.Add(image);
}

With some images, I notice that the file is being held/locked by the application while others are not. I am testing to see if files are held by the application by opening up explorer and trying to rename the file while the application is running. I am able to successfully rename some files. For the files that I can't rename, I get an error message stating "Cannot rename...It is being used by another person or program."

Any ideas why this happens Is there any way to load an image file so that it is not held by the application

-Manny



Visual Studio 20081  
 
 
Unni Ravindranathan - MSFT





PostPosted: Windows Presentation Foundation (WPF), BitmapImage holding an image file? Top

Here is the code Expression Interactive Designer is using to avoid the locking of files, so users could edit Images in other programs:

BitmapImage image = new BitmapImage();

image.BeginInit();

image.CacheOption = BitmapCacheOption.OnLoad;

image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;

image.UriSource = <some uri>;

image.EndInit();

Thanks,
-Unni



 
 
Robert A. Wlodarczyk - MSFT





PostPosted: Windows Presentation Foundation (WPF), BitmapImage holding an image file? Top

IgnoreImageCache should only be used if you’re interested in reading the file from disk every time you render. A good example of this is if your application is pointing to a static image on a website (www.foo.com/image.jpg), but image.jpg keeps changes on some frequency. So, In this case you would want to use IgnorImageCache so that the next time you draw the image then the latest version of image.jpg is loaded up.



If you just want to load an image from a local disk and have the BitmapSource not hold onto the file, then BitmapCacheOption.OnLoad is what you’re looking for.

 
 
Manny06





PostPosted: Windows Presentation Foundation (WPF), BitmapImage holding an image file? Top

I have changed the cache option to BitmapCacheOption.OnLoad.  However, I the application is still holding onto a few files.  Everytime I run the app, the same files are always locked. Is this a bug