2

I have an application that provides print functionality and the ability to do a preview of that print. Print preview is handled using a PrintPreviewDialog. The application is a C# WinForms .Net 4.0 application.

In essence, the function is printing (previewing) a multi-page report, and in this particular instance it is previewing a large number of image files (about 950 at 2 images per page).

In this case, the application is throwing an error at page 462 (so not too far off the total), and at that time the following task manager values are available:

  • Memory (PWS): ~1.6GB
  • Handles: ~480
  • User Objects: ~300
  • GDI Objects: ~1400

I am sure that the memory (although high) is not the issue, as I would expect an out of memory exception. The GDI objects is getting on a bit, but I don't think this is too high (I believe the limit by default is 10000?)

The actual exception message being shown is the ever helpful:

A generic error occurred in GDI+

A few other things to be wary of:

  • This is an application running at a customer's site, so there is no ability to run a debugger
  • The images are first loaded into memory as a collection of Image objects (which explains the high GDI Object count) and this cannot be changed at this stage
  • I have not yet confirmed if this is also an issue when doing a real print, obviously nobody wants to waste all that paper, but I am awaiting the results of a Print to PDF test

Finally, my question is: Are there any limits I should be aware of that might cause this behavior? Such as a Windows limit that might be set in the Registry. Something related to print memory? or a different GDI limit for print?

Are there any further tests I could run to help diagnose the cause?

musefan
  • 47,875
  • 21
  • 135
  • 185

1 Answers1

0

I don't know if it helps, but it may not be the case of system limist, but file lock.

You mentioned that you're using Image to load files into memory. MSDN says here:

The file remains locked until the Image is disposed.

Then if your program tries to touch it, GDI+ goes crazy.

The error you get sometimes appear when saving images to disc, as in here: Image.Save(..) throws a GDI+ exception because the memory stream is closed

Maybe try loading image into MemoryStream with FileShare.ReadWrite option?

Community
  • 1
  • 1
Arie
  • 5,251
  • 2
  • 33
  • 54
  • Actually, I am reluctant to think this is the cause, due to the fact that after I load the file into a MemoryStream. I then draw it to a new Image, and close the MemoryStream - which should eliminate any connection to the file. It could be an initial read that is locked, it's hard to tell, as I said at the moment I cannot change the code to test this. – musefan Jul 09 '13 at 14:34