title: Converting a JPEG image to a byte array - COM exception
body: <p>Using C#, I'm trying to load a JPEG file from disk and convert it to a byte array. So far, I have this code:</p>
  
  <pre><code>static void Main(string[] args)
  {
      System.Windows.Media.Imaging.BitmapFrame bitmapFrame;
  
      using (var fs = new System.IO.FileStream(@"C:\Lenna.jpg", FileMode.Open))
      {
          bitmapFrame = BitmapFrame.Create(fs);
      }
  
      System.Windows.Media.Imaging.BitmapEncoder encoder = 
          new System.Windows.Media.Imaging.JpegBitmapEncoder();
      encoder.Frames.Add(bitmapFrame);
  
      byte[] myBytes;
      using (var memoryStream = new System.IO.MemoryStream())
      {
          encoder.Save(memoryStream); // Line ARGH
  
          // mission accomplished if myBytes is populated
          myBytes = memoryStream.ToArray(); 
      }
  }
  </code></pre>
  
  <p>However, executing line <code>ARGH</code> gives me the message:</p>
  
  <blockquote>
    <p>COMException was unhandled. The handle is invalid. (Exception from
    HRESULT: 0x80070006 (E_HANDLE))</p>
  </blockquote>
  
  <p>I don't think there is anything special about the file <code>Lenna.jpg</code> - I downloaded it from <a href="http://computervision.wikia.com/wiki/File:Lenna.jpg" rel="noreferrer">http://computervision.wikia.com/wiki/File:Lenna.jpg</a>. Can you tell what is wrong with the above code?</p>