title: ASP.NET [ExternalException (0x80004005): A generic error occurred in GDI+.] at bitmap.Save(MStream, ImageFormat.Tiff)
body: <p>I've read hundreds of "A generic error occurred in GDI+" posts and can't quite get the issue straightened out.  I'm trying to insert a page and then apply some text to each page of a multi-page tiff.</p>
  
  <p>If I just want to insert a page without applying any additional text to each page, this code has been working fine no matter how many pages the multi-page tiff contains:</p>
  
  <pre><code>'yada yada yada
  cover.Save(byteStream, ImageFormat.Tiff)
  images.Add(Image.FromStream(byteStream))
  
  'loop through each page of the multi-page tif
  Dim count As Integer = bitmap.GetFrameCount(FrameDimension.Page)
              For idx As Integer = 0 To count - 1
                  bitmap.SelectActiveFrame(FrameDimension.Page, idx)
                  bitmap.Save(byteStream, ImageFormat.Tiff)
                  images.Add(Image.FromStream(byteStream))
              Next
  
  'some stuff to process and save a tif
  </code></pre>
  
  <p>This is the code I'm using to insert the cover and add additional text to each page.  It seems to work fine unless the multi-page tiff has more than about 20 pages:</p>
  
  <pre><code>'yada yada yada
  cover.Save(byteStream, ImageFormat.Tiff)
  images.Add(Image.FromStream(byteStream))
  
  Dim stringFormat As StringFormat = New StringFormat()
  stringFormat.Alignment = StringAlignment.Far
  
  'loop through each page of the multi-page tif
  Dim count As Integer = bitmap.GetFrameCount(FrameDimension.Page)
          For idx As Integer = 0 To count - 1
  
              bitmap.SelectActiveFrame(FrameDimension.Page, idx)
  
              Dim bitmap2 As New Bitmap(bitmap)
              bitmap2.SetResolution(300, 300)
  
              Dim g As Graphics = Graphics.FromImage(bitmap2)
  
              Select Case Trim(Type)
                  Case Is = "X"
                      Dim rectf As New RectangleF(2100, 75, 300, 75)
                      g.DrawString(DocNum, New Font("Arial", 10), Brushes.Black, rectf, stringFormat)
                  Case Is = "Y"
                      Dim rectf As New RectangleF(2100, 150, 300, 75)
                      g.DrawString(DocNum, New Font("Arial", 10), Brushes.Black, rectf, stringFormat)
              End Select
  
              bitmap2.Save(byteStream, ImageFormat.Tiff)
  
              images.Add(Image.FromStream(byteStream))
  
              g.Dispose()
              bitmap2.Dispose()
  
          Next
  
  'some stuff to process and save a tif
  </code></pre>
  
  <p>I get "A generic error occurred in GDI+." "[ExternalException (0x80004005): A generic error occurred in GDI+.]" at bitmap2.Save(byteStream, ImageFormat.Tiff) once I hit about the 20th image. Any thoughts as to what the issue is and how I might resolve it?  It doesn't seem to be a file permission issue.  I've tried using a "using memorystream end using" and the doesn't seem to matter.  Am I missing something obvious? </p>