Skip Ribbon Commands
Skip to main content
Navigate Up
Sign In

Quick Launch

Ralph Arvesen (vertigo blog) > Posts > WPF, binding to an image without file access exceptions
September 05
WPF, binding to an image without file access exceptions
You can run into file access errors when using images in WPF. By default, the .Net Framework locks the image until it's not used and the garbage collector decides to release the reference. For example, the following snippet displays an image but a file access error occurs if the application, or the user, tries to modify or delete the image file from the file system.
<Image Source="d:\vertigo.png" />
You can change how the framework manages the reference to the image with the BitmapImage.CacheOption property. The following snippet specifies that the image is loaded and the reference is released, so the image on the file system can be modified or deleted.
<Image>
  <Image.Source>
    <BitmapImage UriSource="d:\vertigo.png" CacheOption="OnLoad"></BitmapImage>
  </Image.Source>
</Image>
But the UriSource property does not support binding, so you cannot do something simple like the following:
<Image>
  <Image.Source>
    <!-- this does not work -->
    <BitmapImage UriSource="{Binding Path=ImagePath}" CacheOption="OnLoad"></BitmapImage>
  </Image.Source>
</Image>
Instead, you can do your own UriSource binding by creating a converter that takes in a path to the image and returns a BitmapImage object. For example, the following XAML binds the Source property to ImagePath (which is the path to the image), along with a converter that returns a BitmapImage object.
<local:ImagePathConverter x:Key="ImagePathConverter"/>
<Image Source="{Binding Path=ImagePath, Converter={StaticResource ImagePathConverter}}" />
The converter is pretty straightforward: creates a BitmapImage object, sets the CacheOption property, and returns the object. So now you can data bind to an image and still modify or delete the image from the file system without file access errors.
public class ImagePathConverter : IValueConverter
{
    public object Convert(object value, Type targetType, 
        object parameter, System.Globalization.CultureInfo culture)
    {
        // value contains the full path to the image
        string path = (string)value;

        // load the image, specify CacheOption so the file is not locked
        BitmapImage image = new BitmapImage();
        image.BeginInit();
        image.CacheOption = BitmapCacheOption.OnLoad;
        image.UriSource = new Uri(path);
        image.EndInit();

        return image;
    }

    public object ConvertBack(object value, Type targetType, 
        object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException("The method or operation is not implemented.");
    }

Comments

Net Framework Development

Nice code example.... awesome post!!!
System Account on 9/8/2008 2:51 AM

Steve T

Saved me a lot of time - thanks!!
System Account on 3/22/2010 4:03 AM

Developer

I managed to find a way around this problem in WinForms, but I didn't have a clue in WPF.  Your post saved me.  Thanks!!
System Account on 6/8/2010 1:14 PM

FM

Well explained, thanks a lot!
System Account on 6/16/2010 8:31 AM

EO

Very times saving article. Thanks alot.
 on 10/9/2011 4:57 AM

good

thank you !
 on 11/16/2011 8:09 PM

good

thank you !
 on 11/16/2011 8:13 PM

Thanks

Thanks, much appreciated
 on 12/10/2011 8:56 PM

amazing

Just what I needed :). Thank you.
 on 1/22/2012 5:44 PM

Thanks

Exactly what was needed.  Great post, and great piece of code.
 on 5/2/2012 2:00 AM
1 - 10Next

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Title


Body *


CommentUrl


Attachments