Using GDI+ in your .NET programs

To use GDI+ in your .NET based programs you need to do a couple of things before you start on the business of writing the code. First, you need to include a reference to the System.Drawing assembly and secondly, you need to declare that you are using the System.Drawing namespace in your code.

To use GDI+ in your .NET based programs you need to do a couple of things before you start on the business of writing the code. First, you need to include a reference to the System.Drawing assembly and secondly, you need to import the the System.Drawing namespace in your code

Adding a reference to the System.Drawing DLL in Visual Studio is easy. Just right click on the references section in the Solution Explorer and select "Add Reference". The following dialog will be shown.

Figure 1. The Add Reference dialog.

At the top of each code file you should declare which namespaces the code in the file uses. This is done with the using statement as shown in the listing below

 

using System;

using System.Drawing;

//other using statements go here

namespace MyNamespace

{

  //classes go here...

}

At the top of each code file you should declare which namespaces the code in the file uses. This is done with the Imports statement as shown in the listing below

 

Imports System

Imports System.Drawing

'other Import statements go here

Namespace MyNamespace

'classes go here

End Namespace

 

If you use the command line compilers with the .NET SDK the C# compiler automatically adds the references for you.

If you're using the command line compilers with the .NET SDK you must remember to include the references to the System.Drawing DLL on the command line.

If you use the Visual Studio IDE and create a Windows Forms application then the code wizard will automatically include the correct statements for you and you'll be able to use GDI+ right away.

Return to the Beginners Guide index.