Click here to Skip to main content
15,399,959 members
Articles / Programming Languages / C#
Posted 6 Mar 2011

Tagged as


13 bookmarked

Disposing GDI Objects in C#. NET

Rate me:
Please Sign up or sign in to vote.
4.17/5 (14 votes)
9 Mar 2011CPOL5 min read
This article details when, where and how to dispose of objects that create GDI object handles


The aim of this article is to describe the importance of correctly disposing of objects that create GDI handles. It also details the types of objects which create these GDI handles, how and when to dispose of these objects and what can happen if you do not correctly dispose of them.

What are GDI Objects

GDI (Graphics Device Interface) Objects are typically objects that are used to handle drawing on a surface within an application. They may be used to hold image data such as the Bitmap object or to define how a drawing element should be styled as with the Brush object.

If you have ever done any custom drawing, such as that which you might handle with the Paint event of a Panel, then the chances are that you will have used one of these object types before. The question is, have you correctly been disposing of them? Or more importantly, why should you care?

Why it is Important to Dispose of GDI Objects

Unlike the majority of the objects in the .NET Framework, GDI handles are not automatically disposed when your application no longer holds a reference to them. This, as you may have already guessed, can lead to memory allocations that are never released. Over time (application up-time), this could lead to large consumption of memory usage and ultimately an "Out of Memory" exception.

There is however another consideration, which given the vast amounts of RAM computers use as standard, is probably going to be of greater concern - especially if you’re doing a fair amount of custom drawing.

Windows has a limit on the number of GDI handles that a session (i.e., application) is permitted to hold. This value is determined in the system registry, and by default, is set to 10,000 handles (at least this is the case in XP, Vista and 7). This value can be altered by those of you comfortable editing the registry but the maximum that can be applied is 65,536 (Windows 2000 is 16,384).

I would strongly advise you do not rely on this method, as in a production application it would be better to not change any system registry values on a customers machine. The last thing you want is to cause a customer's system problems. Either way, the following registry setting is the one of interest:


*** WARNING ***
Changing the wrong registry values can seriously damage the stability of your Windows system. Do so at your own risk.

The Objects to Look Out For

The following object types are the ones to look out for, as these will cause a GDI handle to be created upon their initialisation:

  • Bitmap
  • Brush
  • DC
  • Enhanced Metafile
  • Enhanced Metafile DC
  • Font
  • Memory DC
  • Metafile
  • Metafile DC
  • Palette
  • Pen
  • Extended Pen
  • Region

(This list was obtained via the Microsoft documentation and can be found here.)

How to Dispose of These Objects

Disposing of these GDI objects can be as simple as a call to the Dispose method of the object as follows:

Bitmap bitmap = new Bitmap("mypic.bmp");

The important thing to remember here is that once you have called the Dispose function, you should not try to access any of the other properties or methods of the disposed object as this will cause an exception. For example, the following would cause you problems:

Bitmap bitmap = new Bitmap("mypic.bmp");

When to Dispose of These Objects

As you can see, it is simple enough to dispose of the object, but the more important question is, when to dispose of it. Well, this of course depends on how and where you declare and use the objects. Consider the following code sample:

void EditPhoto(string filename)
	Bitmap bitmap = new Bitmap(filename);
	//do some image manipulation here

In the above example, the bitmap object is declared within the scope of the function. In these instances, you should always dispose of the object before the function returns. If this is not done, then your application will lose the reference to the bitmap object, and you will lose the chance to dispose of the GDI handle. These kind of scenarios are known as Memory Leaks (which you can Google for more information).

But what if your reference declaration is not local to a function? Well, imagine we have a Windows Form that contains a panel in which we are drawing some basic shapes. We will use the Paint event of the panel control in order to draw on the surface of the panel using a Pen object. For performance reasons, we will want to declare our Pen object reference within the scope of the Window class, rather than inside the Paint event, in order to minimise the expensive process of creating and disposing of the Pen every time the panel needs a re-paint. The following code illustrates this:

public partial class FormPaint : Form {
Pen pen = new Pen(Color.Red);
public FormPaint()
private void panelPaint_Paint(object sender, PaintEventArgs e) 
	e.Graphics.DrawRectangle(pen, new Rectangle(10, 10, 100, 100));
private void FormPaint_FormClosed(object sender, FormClosedEventArgs e)

As you can see in the above example, the Pen reference is declared in the scope of the Form class so we must remember to correctly dispose of the object before the form is closed (disposed), otherwise we lose the reference and lose the chance to dispose of the GDI handle. In this example, this has been overcome by disposing of the Pen object within the FormClosed event of the Form.


I know most of you will probably already be aware of the issues described here, but I had to find out about this pitfall the hard way (when the bugs came calling), so hopefully this will help at least a few people out.

This is my first article, so I will look forward to any (hopefully constructive) criticisms that anyone has to offer.

Thanks for reading.


  • 4th March, 2011: Initial version
  • 9th March, 2011: Slight change to code snippet


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

QuestionHow to resue the disposed bitmap or any thing else? Pin
Rrohit Maheshwari14-Oct-15 20:35
professionalRrohit Maheshwari14-Oct-15 20:35 
GeneralMy vote of 1 Pin
DenisK14-Mar-11 7:29
professionalDenisK14-Mar-11 7:29 
GeneralMy vote of 1 Pin
Michal Brylka10-Mar-11 5:21
MemberMichal Brylka10-Mar-11 5:21 
GeneralMy vote of 1 Pin
musefan11-Mar-11 4:47
Membermusefan11-Mar-11 4:47 
GeneralA small fraction of the complete picture Pin
_groo_10-Mar-11 0:36
Member_groo_10-Mar-11 0:36 
GeneralRe: A small fraction of the complete picture Pin
bilo8111-Mar-11 4:35
Memberbilo8111-Mar-11 4:35 
GeneralJust a clarification [modified] Pin
bilo819-Mar-11 23:28
Memberbilo819-Mar-11 23:28 
GeneralRe: Just a clarification Pin
musefan11-Mar-11 4:42
Membermusefan11-Mar-11 4:42 
GeneralRe: Just a clarification Pin
bilo8111-Mar-11 6:52
Memberbilo8111-Mar-11 6:52 
GeneralDispose method Pin
Juan1R7-Mar-11 23:25
MemberJuan1R7-Mar-11 23:25 
GeneralRe: Dispose method Pin
musefan8-Mar-11 1:15
Membermusefan8-Mar-11 1:15 
GeneralRe: Dispose method Pin
Juan1R8-Mar-11 22:50
MemberJuan1R8-Mar-11 22:50 
GeneralRe: Dispose method Pin
musefan8-Mar-11 23:08
Membermusefan8-Mar-11 23:08 
GeneralFont properties and Font objects Pin
supercat97-Mar-11 9:37
Membersupercat97-Mar-11 9:37 
GeneralRe: Font properties and Font objects Pin
musefan8-Mar-11 1:28
Membermusefan8-Mar-11 1:28 
GeneralRe: Font properties and Font objects Pin
supercat98-Mar-11 4:43
Membersupercat98-Mar-11 4:43 
GeneralRe: Font properties and Font objects Pin
musefan8-Mar-11 6:33
Membermusefan8-Mar-11 6:33 
GeneralMy vote of 3 Pin
Oshtri Deka6-Mar-11 23:03
professionalOshtri Deka6-Mar-11 23:03 
GeneralRe: My vote of 3 Pin
musefan6-Mar-11 23:20
Membermusefan6-Mar-11 23:20 
Questionwhy not using block ??? Pin
senguptaamlan6-Mar-11 20:31
Membersenguptaamlan6-Mar-11 20:31 
AnswerRe: why not using block ??? Pin
Groulien6-Mar-11 20:45
MemberGroulien6-Mar-11 20:45 
GeneralRe: why not using block ??? Pin
musefan6-Mar-11 23:08
Membermusefan6-Mar-11 23:08 
GeneralRe: why not using block ??? Pin
Groulien8-Mar-11 7:15
MemberGroulien8-Mar-11 7:15 
GeneralRe: why not using block ??? Pin
musefan8-Mar-11 7:26
Membermusefan8-Mar-11 7:26 
GeneralRe: why not using block ??? Pin
_groo_10-Mar-11 0:40
Member_groo_10-Mar-11 0:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.