0

I want to show Image in my WPF Window. I have put this code to do so.

<Image  x:Name="ImageControl" Stretch="Fill" Margin="2" Source="{Binding imgSource}"/>

and in code behind I have put,

    public ImageSource imgSource
    {
        get
        {
            logo = new BitmapImage();
            logo.BeginInit();
            logo.UriSource = new Uri(@"C:\MyFolder\Icon.jpg");
            logo.EndInit();                
            return logo;
        }
    }

This code shows image fine but I also should be able to change image runtime, That is, I want to replace Icon.jpg with another Image. MyFolder is the folder path that will contain an Image "Icon.jpg" (Name would always be same). So whenever I try to replace Icon.jpg with anyother Image, I get an error That Image file in Use

Can Anyone suggest how to overcome this issue. Please let me know if I need to clear my question.

Thanks in Anticipation.

1 Answer 1

1
  • Implement INotifyPropertyChanged in the class.

  • Change your property to a "get" "set"

  • And don't forget to set the DataContext.

Here is the code:

public class MyClass : INotifyPropertyChanged
{
    private string imagePath;
    public string ImagePath
    {
        get { return imagePath; }
        set
        {
            if (imagePath != value)
            {
                imagePath = value;
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.UriSource = new Uri(ImagePath);
                bitmapImage.EndInit();
                imgSource = bitmapImage;
            }
        }
    }

    public BitmapImage logo;
    public ImageSource imgSource
    {
        get { return logo; }
        set
        {
            if (logo != value)
            {
                logo = value;
                OnPropertyChanged("imgSource");
            }
        }
    }

    #region INotifyPropertyChanged implementation
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
    #endregion
}

UPDATE

BitmapImage is known to keep the file loaded when passing the path using string. Load with a FileStream instead. BitmapImage as on demand loading capability set by default. To foce the bitmap to load the image on EndInit you have to change the ChacheOption:

using (FileStream stream = File.OpenRead(@"C:\MyFolder\Icon.jpg"))
{
    logo = new BitmapImage();
    logo.BeginInit();
    logo.StreamSource = stream;
    logo.CacheOption = BitmapCacheOption.OnLoad;
    logo.EndInit();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.