none
Metafile not disposing correctly? RRS feed

  • Question

  • Very strange issue here and I'm stumped.

    To start, I'm taking a LocalReport and rendering with a slightly modified version of this: This

    Once I have the rendered pages, I provide the streams to a PrintDocument. It has a PrintPage handler that creates a Metafile with the page stream and draws the metafile content onto the PrintPageEventArgs Graphics.

    Here's the PrintPage:

    if (stream.Length > 0)
    {
        stream.Position = 0;
        using (var pageImage = new Metafile(stream))
        {
            ev.Graphics.DrawImage(pageImage, ev.PageBounds);
        }
    
        stream.Close();
        stream.Dispose();
        GC.Collect();
    }


    I was having a problem with getting a generic GDI exception, but it only seemed to be if I was printing a large enough report and if it was being printed from a Windows XP machine.

    My thought was that the metafile wasn't getting disposed properly. What I decided to do was put the following in a loop:

    for ( loop = 0; loop < 100; loop++)
    {    
        stream.Position = 0;
        using (var pageImage = new Metafile(stream))
        {    
            ev.Graphics.DrawImage(pageImage, ev.PageBounds);
        }
    }

    Here are my observations:

    1. Windows XP: Same problem as before, generic GDI crash. Also note that I tested this with a small version of the report that normally would have rendered fine.

    2. Windows 7: Doesn't crash, but renders strange. The report has a photo in the middle of the first page. It seems to have taken this photo and shrunk it down to about half the size and layered it on top of the correct sized photo. It seemed like the rest of the report rendered fine.

    I'm not really worried about the Windows 7 issue since it renders fine normally. I'm just wondering what I'm supposed to do about the Windows XP problem. Upgrading the XP machine to 7 is an option, but I was hoping to actually fix the issue. My gut feeling says it's an issue with not disposing properly, but I don't know what to do.

    I thought it was related to This, but we seem to have the latest copy of the file.


    • Edited by uler3161 Friday, February 8, 2013 1:50 AM Added clarification on report size.
    • Moved by Mike Feng Wednesday, February 13, 2013 3:08 AM
    Friday, February 8, 2013 1:48 AM

All replies

  • Hi Uler,

    Welcome to the MSDN Forum.

    >>My thought was that the metafile wasn't getting disposed properly.

     How did you verify this? Did you check the memory dump?

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, February 8, 2013 8:25 AM
  • I think the loop code verified there was something wrong. I would have expected it to open the metafile, draw over the image from the previous loop, then dispose of the metafile. And I would have expected that to be successful no matter how many times I looped, but that doesn't seem to be the case.

    I have not checked a memory dump. Do you think that it's an out of memory situation causing the GDI crash? I've read that's a common reason. But I don't recall the process being that close to the memory limit.

    I'll attempt to build a project to reproduce the issue if you think that would help.

    Sunday, February 10, 2013 5:28 PM
  • Hi Uler,

    >>I'll attempt to build a project to reproduce the issue if you think that would help.

    It would be nice, when you finished, post the download link here. Thank you so much.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, February 11, 2013 8:50 AM
  • Ok, I think I have something that you'll be able to reproduce this with.

    https://dl.dropbox.com/u/41587652/TestMetafileCrash.zip

    There is a text box that you enter a integer into. All that does is change the number of loops from my example in the original post. If you leave it at a small number, you should see it work fine. Bump it up to something higher and it will crash. I was seeing it crash at 10 loops on the XP machine I tested on, but you may need more.

    Since the metafile is constructed in a using block I don't see how it's the cause. Is there some memory issue with calling DrawImage multiple times on the PrintPageEventArgs Graphics object? That's the only other place I could see that might be a problem, although I'm only calling it once in the actual application.

    I did watch the memory usage skyrocket as this ran, so something is either not getting cleaned up or is growing into a huge object.

    Monday, February 11, 2013 8:40 PM
  • Hi Uler,

    I am sorry to keep you in this forum so long time. Now I have moved this thread to report control forum, you would get more responses there.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, February 13, 2013 3:08 AM
  • Thanks.

    Wednesday, February 13, 2013 4:07 PM
  • Just wondering if anyone has looked at this yet? I guess I can try to dig deeper and figure out what the problem is, but I was hoping it was something obvious that I'm just overlooking.
    Thursday, February 14, 2013 5:09 PM
  • I did a little more testing. It seems I have better luck if I force a garbage collection after drawing the Metafile image onto the PrintPageEventArgs Graphics, but I've always thought that forcing GC was not something that should be done.

    However, there is still an issue. If I run too many loops, I get this...

    System.ArgumentException: Parameter is not valid.
       at System.Drawing.Image.get_RawFormat()
       at System.Drawing.Graphics.IgnoreMetafileErrors(Image image, Int32& errorStatus)
       at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y, Int32 width, Int32 height)
       at System.Drawing.Graphics.DrawImage(Image image, Rectangle rect)
       at System.Windows.Forms.PrintPreviewControl.OnPaint(PaintEventArgs pevent)
       at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
       at System.Windows.Forms.Control.WmPaint(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.PrintPreviewControl.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

    And what is interesting is that this only seems to happen after all the pages of the document have been drawn to the PrintPageEventArgs.

    Thursday, February 14, 2013 5:52 PM
  • Just wondering if anyone could at least verify that they also have the crashing issues?
    Tuesday, February 26, 2013 8:08 PM