title: Load a BitmapSource and save using the same name in WPF -> IOException
body: <p>When I try to save a BitmapSource that I loaded earlier, a <code>System.IO.IOException</code> is thrown stating another process is accessing that file and the filestream cannot be opened.</p>
  
  <p>If I only save whithout loading earlier, everything works fine.</p>
  
  <p>The loading code:</p>
  
  <pre><code>BitmapImage image = new BitmapImage();
  
  image.BeginInit();
  image.UriSource = uri;
  
  if (decodePixelWidth &gt; 0)
  image.DecodePixelWidth = decodePixelWidth;
  
  image.EndInit();
  </code></pre>
  
  <p>the saving code:</p>
  
  <pre><code>using (FileStream fileStream = new FileStream(Directory + "\\" + FileName + ".jpg", FileMode.Create))
  {
      JpegBitmapEncoder encoder = new JpegBitmapEncoder();
      encoder.Frames.Add(BitmapFrame.Create((BitmapImage)image));
      encoder.QualityLevel = 100;
      encoder.Save(fileStream);
  }
  </code></pre>
  
  <p>It seems like after loading the image data, the file is still locked an can never be overwritten while the application who opened it is still running. Any ideas how to solve this? Thanks alot for any solutions.</p>