'Force WPF BitmapImage to reload from its UriSource

I am trying to build a custom control, based on Image, which simply makes the image reload (from the same URI) on a timer. The original image is displaying fine, but it doesn't seem to refresh.

And here is custom control:

public class RefreshImage : Image
{
    protected override void OnInitialized(EventArgs e)
    {
        base.OnInitialized(e);
        this.Loaded += RefreshImage_Loaded;
    }

    private void RefreshImage_Loaded(object sender, RoutedEventArgs e)
    {
        var timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromSeconds(1);
        timer.Tick += RefreshImage_Tick;
        timer.Start();
    }

    private void RefreshImage_Tick(object sender, System.EventArgs e)
    {
        var bm = (BitmapImage) Source;
        bm.InvalidateProperty(BitmapImage.UriSourceProperty);
    }
 }

And I'm using it like so:

  <custom:RefreshImage>
      <Image.Source>
          <BitmapImage UriCachePolicy="NoCacheNoStore"
                       CreateOptions="IgnoreImageCache" CacheOption="None"
                       UriSource="{Binding Uri}"/>
      </Image.Source>
  </custom:RefreshImage>

Documentation for InvalidateProperty seems to indicate it's exactly what I need:

You can also use InvalidateProperty to force re-evaluation of a binding against a data source that is not able to implement the recommended INotifyPropertyChanged notification mechanism.

Raising an INotifiyPropertyChanged.PropertyChanged event on the Uri also does not trigger the image to reload.

wpf


Solution 1:[1]

Seems that BitmapSource are, in general, immutable objects. Once they have finished being initialized any further property changes are ignored. I suspect this includes the UriSource.

This following phase is repeated often in this MSDN post on creating custom ImageSources:

The ISupportInitialize interface is used to snap the values of the properties when initialization is complete. Further changes to the properties are ignored.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Glorfindel