Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 2.0
System.Drawing
Graphics Class

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Graphics Class
Encapsulates a GDI+ drawing surface. This class cannot be inherited.

Namespace: System.Drawing
Assembly: System.Drawing (in system.drawing.dll)

Visual Basic (Declaration)
Public NotInheritable Class Graphics
    Inherits MarshalByRefObject
    Implements IDeviceContext, IDisposable
Visual Basic (Usage)
Dim instance As Graphics
C#
public sealed class Graphics : MarshalByRefObject, IDeviceContext, IDisposable
C++
public ref class Graphics sealed : public MarshalByRefObject, IDeviceContext, IDisposable
J#
public final class Graphics extends MarshalByRefObject implements IDeviceContext, IDisposable
JScript
public final class Graphics extends MarshalByRefObject implements IDeviceContext, IDisposable
XAML
Not applicable.

The Graphics class provides methods for drawing objects to the display device. A Graphics is associated with a specific device context.

You can obtain a Graphics object by calling the Control.CreateGraphics method on an object that inherits from System.Windows.Forms.Control, or by handling a control's Control.Paint event and accessing the Graphics property of the System.Windows.Forms.PaintEventArgs class. You can also create a Graphics object from an image by using the FromImage method. For more information about creating a Graphics object, see How to: Create Graphics Objects for Drawing.

You can draw many different shapes and lines by using a Graphics object. For more information about how to draw lines and shapes, see the specific DrawGraphicalElement method for the line or shape you want to draw. These methods include DrawLine, DrawArc, DrawClosedCurve, DrawPolygon, and DrawRectangle. For more information about how to draw lines and shapes, see Using a Pen to Draw Lines and Shapes and Using a Brush to Fill Shapes.

You can also draw images and icons by using the DrawImage and DrawIcon methods, respectively. For more information about how to draw images with a Graphics object, see Working with Images, Bitmaps, and Metafiles.

The following code example is designed for use with Windows Forms. It requires PaintEventArgse, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates an image from the SampImag.jpg file in the folder of the example.

  • Creates a point at which to draw the upper-left corner of the image.

  • Draws the unscaled image to the screen by using a Graphics object.

Visual Basic
Private Sub DrawImagePointF(ByVal e As PaintEventArgs)

    ' Create image.
    Dim newImage As Image = Image.FromFile("SampImag.jpg")

    ' Create point for upper-left corner of image.
    Dim ulCorner As New PointF(100.0F, 100.0F)

    ' Draw image to screen.
    e.Graphics.DrawImage(newImage, ulCorner)
End Sub
C#
private void DrawImagePointF(PaintEventArgs e)
{
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create point for upper-left corner of image.
    PointF ulCorner = new PointF(100.0F, 100.0F);
             
    // Draw image to screen.
    e.Graphics.DrawImage(newImage, ulCorner);
}
C++
private:
   void DrawImagePointF( PaintEventArgs^ e )
   {
      // Create image.
      Image^ newImage = Image::FromFile( "SampImag.jpg" );

      // Create point for upper-left corner of image.
      PointF ulCorner = PointF(100.0F,100.0F);

      // Draw image to screen.
      e->Graphics->DrawImage( newImage, ulCorner );
   }
System.Object
   System.MarshalByRefObject
    System.Drawing.Graphics
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Disposing a Graphics object      Harry Pfleger ... Tim Scarfe   |   Edit   |   Show History

When you're finished with a Graphics object, you should always call its Dispose() method to ensure that all resources are freed. Simply letting an object variable go out of scope doesn't ensure that the resources used by the object are freed. Graphics objects can use considerable resources.

In C# you could instead use the using statement, if applicable.

 

I am not sure if that is correct. If you are given a graphics object (say, by a Paint event), I think you should not dispose it (trs-although I think calling Dispose() is a good idea when you got the Graphics instance from CreateGraphics() instead of an event).

Tags What's this?: Add a tag
Flag as ContentBug
Disposing a Graphics object      Gazsi Robert   |   Edit   |   Show History

I had an issue with a Bitmap object that was used by a Graphics object to render onto. The problem was that the Bitmap object didn't had a using statement or a call to Dispose() method, so this resource was to be disposed only when it was garbage collected. The Graphics instance was wrapped with a using statement, but not the Bitmap one.
Similar issues can arise if using other graphic objects together with a Graphics instance like I had.
So be careful to dispose (or wrap with using statements) any IDisposable objects that you use with a Graphics instance.

Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker