C# – System.OutOfMemoryException: Out of memory (GDI)


After installing my program on a windows vista premium, I'm getting the following exception.

The view that must be shown contains following controls: 2 textboxes, 3 labels, a button and linkbutton.

System.OutOfMemoryException: Out of memory.
   at System.Drawing.Graphics.FromHdcInternal(IntPtr hdc)
   at System.Windows.Forms.PaintEventArgs.get_Graphics()
   at System.Windows.Forms.Control.PaintException(PaintEventArgs e)
   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
   at System.Windows.Forms.Control.OnPrint(PaintEventArgs e)
   at System.Windows.Forms.Control.WmPrintClient(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ContainerControl.WndProc(Message& m)
   at System.Windows.Forms.Form.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)

Somebody had the same issue? How to solve it?

Best Solution

This is not necessarily an OOM error, GDI has a habit of throwing Out Of Memory whenever it throws an exception.

To quote Microsoft "GDI+ likes to return OutOfMemoryExceptions in cases that have nothing to do with memory". A nasty little 'idiosyncracy'

See here for details

Related Question