A generic error occured in GDI+


  • Hello, 

    We have a big app running on Windows Server 2003 R2 SP2 Standard x64 Edition .

    Problem is that we dont know where it happens but from time to time we have in log of errors this kind of error:

    System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.

      at System.Drawing.Graphics.CheckErrorStatus(Int32 status)
      at System.Drawing.Graphics.DrawLine(Pen pen, Int32 x1, Int32 y1, Int32 x2, Int32 y2)
      at System.Windows.Forms.GroupBox.DrawGroupBox(PaintEventArgs e)
      at System.Windows.Forms.GroupBox.OnPaint(PaintEventArgs e)
      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.GroupBox.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)

    Do you know what can be the reason? application is not compiled for x64 platform is compiled for 'AnyCPU'.Another thing is that application is running via Citrix. And under .net framework 2.0

    Thursday, May 08, 2008 9:46 AM

All replies

  • CristiUrsachi,


    Based on your post, the error "generic error occurred in GDI+" with the stack trace information is too general to catch the problem. It seems that there is something wrong with the GroupBox control when drawing the line. I would like to provide you an example with the same error on PictureBox control in the thread below:


    Image.FromFile vs. Image.FromStream


    GDI+, and therefore the System.Drawing namespace, may defer the decoding of raw image bits until the bits are required by the image. Additionally, even after the image has been decoded, GDI+ may determine that it is more efficient to discard the memory for a large Bitmap and to re-decode later. Therefore, GDI+ must have access to the source bits for the image for the life of the Bitmap or the Image object.

    To retain access to the source bits, GDI+ locks any source file, and forces the application to maintain the life of any source stream, for the life of the Bitmap or the Image object. You can get the further related workaround in KB 814675


    Hope that can provide you some idea.

    Monday, May 12, 2008 5:47 AM
  • Thx Bruno for your answer, but as you see this is full stack of error and all classes from where is raise the exception is of .net framework. So your advices is not possible to put in practice since we cannot modify/look in GroupBox class. What I suppose is because of citrix enveronment, but I'm not sure what can be the cause, thats why I asked if somebody else have this kind of problem?

    Monday, May 12, 2008 10:39 AM
  • I'm getting the same error but I found out that two things had to be present for it to happen. One, our applications have to be run from within a remote citrix environment. Two, windows must be configured to lock the session after so much time of inactivity or you have to lock it manually using Ctrl-F1 (Alt-Ctrl-Del/Windiows Key are not available for the remote machine due to windows security).


    Once we did that, when we logged back in the exception would be there every time.


    Once the GDI error is trown, even if you ignore the error (which we handled via the MyApplication_UnhandledException event), the GroupBox shows up as a red box with an X through it. I haven't noticed if there are any other side effects to the program yet, just that they have to restart the application to correct drawing of the GroupBoxes.


    To handle the MyApplication_UnhandledException event, open the project properties and make sure "Enable Application Framework" checked. Then click on the "View Application Events" button at the bottom to open the file where the event will be located. Using the class dropdown at the top left choose "MyApplication Events", and using the method dropdown on the right choose "Unhandled Exception". This will create your method stub. The code below is what we used to handle the error.


    Code Snippet

    Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
        If e.Exception.GetType().FullName = "System.Runtime.InteropServices.ExternalException" And e.Exception.Message = "A generic error occurred in GDI+." Then
            e.ExitApplication = False
            MessageBox.Show("An graphical error has occured due to the combination of Citrix, and Windows locking the current session. This will cause some elements displaying incorrectly, and while it should not affect the application's ability to run, these elements will not display correctly until the application is restarted.")
        End If
    End Sub



    Tuesday, May 27, 2008 2:56 PM
  • These problems could be caused by improperly accessing a control or form from another thread.
    Wednesday, May 28, 2008 1:42 PM
  • Thank you Yarx, I think that is the my case too...about why appear exception, did you post/ask on citrix support, even I think is .net framework problem in fact, isnt it?
    Thursday, June 05, 2008 6:49 AM
  • Hello,

    I am getting the same problem on same environment (Windows Serer 2003 R2 SP2 Standered x64 Edition).

    Please reply me for the solution, if you are able to solve this.

    Monday, August 25, 2008 12:36 PM
  • Does anyone know if there is a fix for this?  Service pack etc?
    Wednesday, March 18, 2009 10:29 AM
  • Hi ..
    I am facing the same problem. I think it has something to do with Resize of components or virtual memory problems.
    Main problem is i am not able to catch it and it is thrown as an unhandled exception on screen.
    Monday, May 18, 2009 10:41 AM
  • If anyone can produce a simple 'one form test app' that recreates the problem then I'll send it to Microsoft partner support and get them to look into it.  I've tried recreating it in a test app but with no luck as of yet.
    Friday, May 29, 2009 9:24 AM
  • Hi, have you ever been able to resolve this problem? We recently migrated to a Windows 2008 server farm 64 bit with Citrix running our 2.0 winform application and now the gdi+ error are getting nasty causing groupboxes to break the user session. It not only happens after locking/unlocking a session, I also reproduced it when getting disconnected and reconnect to the citrix session, then the error occurs. I'd like some suggestions on how we could get rid of this problem.
    Friday, November 06, 2009 12:02 AM
  • Hi, we're having the exact same problem unfortunately and it's on Windows server 2008. Have you gotten any resolution yet? Would be interested to find out!
    Friday, November 06, 2009 12:05 AM
  • Hi,

    Bram L, did you solve your problem? I have got the same trouble. If you solved it let me know how you did it.

    Monday, November 16, 2009 12:54 PM
  • I've been able to create a simple 1 form application that exhibits this..

    Remember, you much run the application remotely (using remote desktop) and lock the remote terminal, once you unlock the remote terminal, the GDI+ exception happens.


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    namespace GDI_Test
        public partial class Form1 : Form
            public Form1()
                this.mMsgTimer.Tick += new EventHandler(mMsgTimer_Tick);
            void mMsgTimer_Tick(object sender, EventArgs e)
                this.mCurrText.Text = DateTime.Now.Ticks.ToString();
            private void Form1_Load(object sender, EventArgs e)
        class testTB: System.Windows.Forms.TextBox
            public testTB()
                this.SetStyle(ControlStyles.UserPaint, true);
            protected override void OnPaint(PaintEventArgs e)
    Tuesday, March 02, 2010 12:48 AM
  • Handle exception through try - catch - finally
    call this.Refresh(); in catch block and dispose graphics object in finally block

    For Example :
    protected override void OnPaint(PaintEventArgs e)

                Graphics g = e.Graphics;
                     //Perform your paint
                Catch(Exception Ex)
    Friday, September 09, 2011 1:46 PM