title: Disposing a memory stream properly (WPF Image Conversion)
body: <p>Can anyone tell me how to best dispose of a memory stream?  previously, I had this and everything worked fine:</p>
  
  <pre><code>MemoryStream strmImg = new MemoryStream(profileImage.Image);
  BitmapImage myBitmapImage = new BitmapImage();
  myBitmapImage.BeginInit();
  myBitmapImage.StreamSource = strmImg;
  myBitmapImage.DecodePixelWidth = 200;
  myBitmapImage.DecodePixelWidth = 250;
  myBitmapImage.EndInit();
  this.DemographicInformation.EmployeeProfileImage = myBitmapImage;
  </code></pre>
  
  <p>I later realized, that I'm going to have a memory leak as the <code>MemoryStream</code> implements <code>IDisposable</code> and should be disposed of after I use it, which led me to this implementation:</p>
  
  <pre><code>using(MemoryStream strmImg = new MemoryStream(profileImage.Image))
  {
      BitmapImage myBitmapImage = new BitmapImage();
      myBitmapImage.BeginInit();
      myBitmapImage.StreamSource = strmImg;
      myBitmapImage.DecodePixelWidth = 200;
      myBitmapImage.DecodePixelWidth = 250;
      myBitmapImage.EndInit();
      this.DemographicInformation.EmployeeProfileImage = myBitmapImage;
  }
  </code></pre>
  
  <p>The Problem is in this line of code:</p>
  
  <pre><code> myBitmapImage.StreamSource = strmImg;
  </code></pre>
  
  <p>My assumption is this is referencing the memory location, and the dispose obviously cleans up that location, and it worked in the past because it was never disposed of properly.</p>
  
  <p>My question is, how do I use <code>MemoryStream</code> and dispose it properly after use, while still keeping the converted data (<code>Image</code>) that I need?</p>