title: Cannot render image to HttpContext.Response.OutputStream
body: <p>Basically I am trying to render a simple image in an ASP.NET handler:</p>
  
  <pre><code>public void ProcessRequest (HttpContext context)
  {
      Bitmap image = new Bitmap(16, 16);
      Graphics graph = Graphics.FromImage(image);
  
      graph.FillEllipse(Brushes.Green, 0, 0, 16, 16);
  
      context.Response.ContentType = "image/png";
      image.Save(context.Response.OutputStream, ImageFormat.Png);
  }
  </code></pre>
  
  <p>But I get the following exception:</p>
  
  <pre><code>System.Runtime.InteropServices.ExternalException: A generic error
  occurred in GDI+.
      at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder,
      EncoderParameters encoderParams)
  </code></pre>
  
  <p>The solution is to use this instead of having image write to OutputStream:</p>
  
  <pre><code>MemoryStream temp = new MemoryStream();
  image.Save(temp, ImageFormat.Png);
  byte[] buffer = temp.GetBuffer();
  context.Response.OutputStream.Write(buffer, 0, buffer.Length);
  </code></pre>
  
  <p>So I'm just curious as to why the first variant is problematic?</p>
  
  <p>Edit: The HRESULT is 80004005 which is just "generic".</p>