title: Image.FromStream throws ArgumentException: Parameter is not valid
body: <p>I'm trying to output an image to the output stream through an HttpHandler but it keeps throwing an ArgumentException. I've googled the issue and tried so many things but I still couldn't fix the problem. Anyway, here's the code:</p>
  
  <pre><code>    public void ProcessRequest(HttpContext context)
      {
          Int32 imageId = context.Request.QueryString["id"] != null ? 
              Convert.ToInt32(context.Request.QueryString["id"]) : default(Int32);
  
          if (imageId != 0)
          {
              //context.Response.ContentType = "image/jpeg";
              Byte[] imageData = this._imageInfoManager.GetTradeMarkImage(imageId);
  
              using (MemoryStream ms = new MemoryStream(imageData, 0, imageData.Length))
              {
                  using (Image image = Image.FromStream(ms, true, true)) //this line throws
                  {
                      image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
                  }
              }
          }
          throw new ArgumentException("Image could not be found.");
      }
  </code></pre>
  
  <p>Note that the imageData byte array is not empty and the memory stream is being filled up correctly. Any thoughts?</p>
  
  <p><strong>UPDATE:</strong> 
  Here's the code for <code>GetTradeMarkImage</code>... Note that the images are stored in the an SQL Server database in the <code>image</code> format. </p>
  
  <pre><code>    public Byte[] GetTradeMarkImage(Int32 id)
      {
          object result = DB.ExecuteScalar(SqlConstants.SqlProcedures.Request_GetImageById, id);
  
          if (result != null)
          {
              using (MemoryStream ms = new MemoryStream())
              {
                  BinaryFormatter bf = new BinaryFormatter();
                  bf.Serialize(ms, result);
                  return ms.ToArray();
              }                
          }
          return null;
      }
  </code></pre>