title: How to release Image from Image Source in WPF
body: <p>Am loading image like below</p>
  
  <p>XAML</p>
  
  <pre><code>&lt;Image Stretch="None" Grid.Row="16" Height="70" HorizontalAlignment="Left" Name="imgThumbnail" VerticalAlignment="Top" Width="70" Grid.RowSpan="3" Margin="133,1,0,0" Grid.Column="2" Grid.ColumnSpan="2" /&gt;
  </code></pre>
  
  <p>CodeBehind</p>
  
  <pre><code>if (Path.GetFileNameWithoutExtension(filePath).ToLower().Contains(slugName.ToLower() + "_70x70"))
  {
      imgThumbnail.BeginInit();
      imgThumbnail.Stretch = Stretch.UniformToFill;
      imgThumbnail.Source = new BitmapImage(new Uri(filePath));
      imgThumbnail.EndInit();
      count = count + 1;
  }
  </code></pre>
  
  <p>Above code work fine , now I have a delete button next to my thumbnail, if delete button called I suppose to delete all the images from the source location.</p>
  
  <p>Here is the code to delete the image files</p>
  
  <pre><code>internal int Remove(string slugName, DirectoryInfo outputFolder)
  {
      Helper.MetadataView.imgThumbnail.Source = null;
  
      foreach (string filePath_ToBeDeleted in filePathList_ToBeDeleted)
      {
          if (File.Exists(filePath_ToBeDeleted))
          {
              Helper.MetadataView.imgThumbnail.IsEnabled = false;
              File.Delete(filePath_ToBeDeleted);
              count += 1;
              }
          }
          return count;
      }
      return 0; // slugName == null
  }
  </code></pre>
  
  <p>I tried to source to be null and delete, but it throws exception like below</p>
  
  <p>The process cannot access the file '\serv1\Dev\Images\730_Test4_0406_70x70.jpg' because it is being used by another process.</p>
  
  <p>Am not sure how to dispose, please someone guide me.</p>