title: A generic error occurred in GDI+ exception when trying to save image into MemoryStream
body: <p>I am using C# windows form.</p>
  
  <p>My code :</p>
  
  <pre><code>private void Openbutton_Click(object sender, EventArgs e)
  {
          OpenFileDialog openFileDialog = new OpenFileDialog();
          if (openFileDialog.ShowDialog() == DialogResult.OK)
          {
              SurveyDiagrampictureBox.Image = Bitmap.FromFile(openFileDialog.FileName);
  
              MemoryStream memoryStream = new MemoryStream();
              SurveyDiagrampictureBox.Image.Save(memoryStream, ImageFormat.Jpeg);
              SurveyDiagram = memoryStream.GetBuffer();
          }
  }
  </code></pre>
  
  <p>It doesn't always occur, the exception throws when stepping to this line :             <code>SurveyDiagrampictureBox.Image.Save(memoryStream, ImageFormat.Jpeg);</code></p>
  
  <p>Exception message :</p>
  
  <blockquote>
    <p>An unhandled exception of type
    'System.Runtime.InteropServices.ExternalException' occurred in
    System.Drawing.dll</p>
    
    <p>Additional information: A generic error occurred in GDI+.</p>
  </blockquote>