title: Loading, saving, and then loading an image again throws "A generic error occurred in GDI+"
body: <p>This <a href="https://stackoverflow.com/search?q=A+generic+error+occurred+in+GDI%2B.">seems to be in infamous error</a>. I remember getting it a while back for different code, but <em>it's back</em>, with a vengeance, but with some new code that I can't seem to figure out.<br />
  It's definitely <code>generic</code>, for sure!</p>
  
  <h3>The Goal</h3>
  
  <p>I'm trying to build a form that allows a user to:</p>
  
  <ol>
  <li>Select an image.</li>
  <li>Press save (which closes the form), and saves the image to a <code>byte[]</code> into a database.</li>
  <li>Open the form (which loads the image from the <code>byte[]</code>).</li>
  <li>Allow them to press save again.</li>
  <li>Allow them to open the form again, displaying the image (again).</li>
  </ol>
  
  <p>A pretty standard load/save scenario.</p>
  
  <h3>The Problem</h3>
  
  <p>Everything works fine regarding loading and saving to the SQL Server. The problem I'm getting is to do with repeatedly loading and saving to and from a <code>byte[]</code> even though I'm using the same settings. Take a look at this code which I mocked up to demonstrate the problem:</p>
  
  <pre><code>static void Main(string[] args)
  {
      // Load the image
      var initialImage = (Bitmap)Bitmap.FromFile(@"D:\picture.jpg");
  
      // Save to a memory stream and get the bytes
      var initialImageBytes = SaveImageToBytes(initialImage);
  
      // Load again from this saved image
      Bitmap byteLoadedImage = LoadImageFromBytes(initialImageBytes);
  
      // Save again to bytes, throws "A generic error occurred in GDI+."
      var secondaryImageBytes = SaveImageToBytes(byteLoadedImage);
  }
  
  private static byte[] SaveImageToBytes(Bitmap image)
  {
      byte[] imageBytes;
      using (MemoryStream imageStream = new MemoryStream())
      {
          image.Save(imageStream, image.RawFormat);
          // "A generic error occurred in GDI+." thrown when saved second time
          imageBytes = imageStream.ToArray();
      }
  
      return imageBytes;
  }
  
  private static Bitmap LoadImageFromBytes(byte[] bytes)
  {
      using (MemoryStream imageStream = new MemoryStream(bytes))
      {
          Bitmap image = (Bitmap)Bitmap.FromStream(imageStream);
          return image;
      }
  }
  </code></pre>
  
  <p>The error <code>A generic error occurred in GDI+.</code> is thrown as the image is saved once more to the <code>MemoryStream</code> the second time round. I checked this wasn't to do with the <code>RawFormat</code> by inspecting the value before the first save, and before the second:</p>
  
  <pre><code>1st Save : {b96b3cae-0728-11d3-9d7b-0000f81ef32e}
  2nd Save : {b96b3cae-0728-11d3-9d7b-0000f81ef32e}
  </code></pre>
  
  <p>The values are identical, so it can't be a problem with it losing the <code>ImageFormat</code> information.</p>
  
  <p>Can anyone help debug this problem? The code sample I used is tested with a JPEG, and <a href="http://www.codesleuth.co.uk/images/picture.jpg" rel="nofollow noreferrer">you can get it here</a>.</p>