title: How to dispose BitmapImage cache?
body: <p>I'm facing a memory leak issue. The leak comes from here:</p>
  
  <pre><code>public static BitmapSource BitmapImageFromFile(string filepath)
  {
      BitmapImage bi = new BitmapImage();
  
      bi.BeginInit();
      bi.CacheOption = BitmapCacheOption.OnLoad; //here
      bi.CreateOptions = BitmapCreateOptions.IgnoreImageCache; //and here
      bi.UriSource = new Uri(filepath, UriKind.RelativeOrAbsolute);
      bi.EndInit();
  
      return bi;
  }
  </code></pre>
  
  <p>I have a <code>ScatterViewItem</code>, which contains an <code>Image</code>, and the source is the <code>BitmapImage</code> of this function. </p>
  
  <p>The actual thing is a lot more complex than this, so I can't simply put an Image into it. I also can't use the default load options, as the image file might be deleted and hence will face some permission issue accessing the file during deletion.</p>
  
  <p>The problem occurs when I close the <code>ScatterViewItem</code>, which in turn closes the <code>Image</code>. However, the cached memory isnt cleared. So after many cycles, the memory consumption is pretty big.</p>
  
  <p>I tried setting <code>image.Source=null</code> during the <code>Unloaded</code> function, but it didn't clear it.</p>
  
  <p>How do I clear the memory correctly during unloading?</p>