Jordan
Jordan

Reputation: 9911

Setting and Images CreatOptions and CacheOption in XAML

I want to set my CreateOptions and CacheOption like so:

img = new BitmapImage();
img.BeginInit();
img.CacheOption = BitmapCacheOption.OnDemand;
img.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
img.UriSource = new Uri("C:\\Temp\\MyImage.png", UriKind.Absolute);
img.EndInit();

Is there a way to do this decoratively in XAML? Or do I have to resort to code behind?

Upvotes: 3

Views: 941

Answers (2)

Sandwich
Sandwich

Reputation: 356

I was getting an error related to missing key using the answer provided by Sheridan. Changed to this and it works for me now

[ValueConversion(typeof(string), typeof(ImageSource))]
public class FilePathToImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value.GetType() != typeof(string) || targetType != typeof(ImageSource))
            return false;

        string filePath = value as string;
        if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
            return DependencyProperty.UnsetValue;

        BitmapImage image = new BitmapImage();
        try
        {
            image.BeginInit();
            image.UriSource = new Uri(filePath);
            image.CacheOption = BitmapCacheOption.OnLoad;
            image.EndInit();
        }
        catch (Exception ex)
        {
            return DependencyProperty.UnsetValue;
        }

        return image;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return DependencyProperty.UnsetValue;
    }
}

Upvotes: 0

Sheridan
Sheridan

Reputation: 69979

You can put that inside an IValueConverter:

[ValueConversion(typeof(string), typeof(ImageSource))]
public class FilePathToImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value.GetType() != typeof(string) || targetType != typeof(ImageSource)) return false;
        string filePath = value as string;
        if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath)) return DependencyProperty.UnsetValue;
        BitmapImage image = new BitmapImage();
        try
        {
            using (FileStream stream = File.OpenRead(filePath))
            {
                image.BeginInit();
                image.StreamSource = stream;
                image.CacheOption = BitmapCacheOption.OnDemand;
                image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
                image.EndInit();
            }
        }
        catch { return DependencyProperty.UnsetValue; }
        return image;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return DependencyProperty.UnsetValue;
    }
}

You could then use it in XAML like this:

<Image Source="{Binding SomeFilePath, Converter={StaticResource 
    FilePathToImageConverter}, Mode=OneWay}" />

In code somewhere:

SomeFilePath = "C:\\Temp\\MyImage.png";

Upvotes: 2

Related Questions