Export (0) Print
Expand All

Graphics Class

Encapsulates a GDI+ drawing surface. This class cannot be inherited.

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

public sealed class Graphics : MarshalByRefObject, 
	IDeviceContext, IDisposable

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, Icons, and Metafiles.

The following code example is designed for use with Windows Forms and requires a PaintEventArgs object. The PaintEventArgs object is named e and is a parameter of the Paint event handler. The code performs the following actions:

  • Creates an image from a JPEG file. The file is named SampImag.jpg and is located 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.

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);
}

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 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft