title: "A generic error occurred in GDI+" when loading an image
body: <p>Yes another one of those... Stack trace (meaningful part):</p>
  
  <pre><code>System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+
     at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)
     at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement)
     at System.Drawing.Image.FromStream(Stream stream)
  </code></pre>
  
  <p>This is happening when trying to open image files uploaded by end-users. The code is approx this (stripped down to essential parts):</p>
  
  <pre><code>HttpPostedFile file = Request.Files[name];
  Stream stream = file.InputStream;
  byte[] bytes = new byte[file.ContentLength];
  stream.Read(bytes, 0, file.ContentLength);
  return bytes;
  </code></pre>
  
  <p>which is then used in</p>
  
  <pre><code>MemoryStream stream = new MemoryStream(bytes);
  return Image.FromStream(stream);
  </code></pre>
  
  <p>So essentially it bails on trying to load image from a memory stream. This doesn't happen always. In fact I cannot reproduce the issue myself, but it happened about 20 times in the last 2 weeks to a few different users. I do not have access to the actual files that triggered the problem.</p>
  
  <p>Any clues to what might be happening here would be appreciated.</p>