Share via


Creating a Graphics Object

To draw graphics to any display device, you need a Graphics object. A Graphics object is associated with a drawing surface, typically the client area of a Form object. Use one of the following techniques to get a Graphics object to draw to a Form.

Using the CreateGraphics Method

Use the Form.CreateGraphics method to create a Graphics object that draws to a Form object.

The following example creates a subclass of the Form class, calls its CreateGraphics method, and uses the resulting Graphics object to draw a rectangle in the client area of the form:

Imports System
Imports System.Windows.Forms
Imports System.Drawing

'Create a Class that inherits from System.Windows.Forms.Form. 
Class myForm 
   Inherits Form 
   
   'Override myForm's OnClick event. 
   Protected Overrides Sub OnClick(ByVal e As EventArgs) 

      'Use the CreateGraphics method to create a Graphics object. 
       Dim formGraphics As Graphics
       formGraphics = Me.CreateGraphics

      'Create a red brush. 
      Dim redBrush As new SolidBrush(Color.Red)

      'Draw a rectangle on the form. 
      formGraphics.FillRectangle(redBrush, 0, 0, 100, 100)
   End Sub 'OnClick

   Public Shared Sub Main() 
      Application.Run(new myForm())
   End Sub 'Main

End Class

[C#]
using System;
using System.Windows.Forms;
using System.Drawing;

//Create a Class that inherits from System.Windows.Forms.Form. 
class myForm : Form {
   
   //Override myForm's OnClick event. 
  protected override void OnClick(EventArgs e) {

      //Use the CreateGraphics method to create a Graphics object. 
      Graphics formGraphics = this.CreateGraphics();

      //Create a red brush. 
      SolidBrush redBrush = new SolidBrush(Color.Red);

      //Draw a rectangle on the form. 
      formGraphics.FillRectangle(redBrush, 0, 0, 100, 100);
   }

   public static void Main() {
      Application.Run(new myForm());
   }
}

Overriding the OnPaint Event Handler

The OnPaint method of a Form class receives a PaintEventArgs object as a parameter. One of the members of this object is a Graphics object associated with the form.

The following example overrides the OnPaint method of a Form class and uses the Graphics object from its PaintEventArgs parameter to draw a rectangle in the client area of the form:

Imports System.Windows.Forms
Imports System.Drawing

'Create a Class that inherits from System.Windows.Forms.Form. 
Class myForm 
   Inherits Form 
   
   'Override myForm's OnPaint event handler. 
   Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) 

      'Use the Graphics object from the PaintEventArgs object. 
       Dim formGraphics As Graphics
       formGraphics = e.Graphics

      'Create a red brush. 
      Dim redBrush As new SolidBrush(Color.Red)

      'Draw a rectangle on the form. 
      formGraphics.FillRectangle(redBrush, 0, 0, 100, 100)
   End Sub 'OnClick

   Public Shared Sub Main() 
      Application.Run(new myForm())
   End Sub 'Main

End Class

[C#]
using System;
using System.Windows.Forms;
using System.Drawing;

//Create a Class that inherits from System.Windows.Forms.Form. 
class myForm : Form {
   
   //Override myForm's OnPaint event. 
  protected override void OnPaint(PaintEventArgs e) {

      //Get the Graphics object from the PaintEventArgs object. 
      Graphics formGraphics = e.CreateGraphics();

      //Create a red brush. 
      SolidBrush redBrush = new SolidBrush(Color.Red);

      //Draw a rectangle on the form. 
      formGraphics.FillRectangle(redBrush, 0, 0, 100, 100);
   }

   public static void Main() {
      Application.Run(new myForm());
   }
}