Graphics Class

Definition

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

public ref class Graphics sealed : MarshalByRefObject, IDisposable, System::Drawing::IDeviceContext
public ref class Graphics sealed : MarshalByRefObject, IDisposable
public sealed class Graphics : MarshalByRefObject, IDisposable, System.Drawing.IDeviceContext
[System.Runtime.InteropServices.ComVisible(false)]
public sealed class Graphics : MarshalByRefObject, IDisposable
type Graphics = class
    inherit MarshalByRefObject
    interface IDisposable
    interface IDeviceContext
[<System.Runtime.InteropServices.ComVisible(false)>]
type Graphics = class
    inherit MarshalByRefObject
    interface IDisposable
type Graphics = class
    inherit MarshalByRefObject
    interface IDeviceContext
    interface IDisposable
Public NotInheritable Class Graphics
Inherits MarshalByRefObject
Implements IDeviceContext, IDisposable
Public NotInheritable Class Graphics
Inherits MarshalByRefObject
Implements IDisposable
Inheritance
Attributes
Implements

Examples

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 = PointF(100.0F,100.0F);

      // Draw image to screen.
      e->Graphics->DrawImage( newImage, ulCorner );
   }
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);
}
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

Remarks

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

Note

In .NET 6 and later versions, the System.Drawing.Common package, which includes this type, is only supported on Windows operating systems. Use of this type in cross-platform apps causes compile-time warnings and run-time exceptions. For more information, see System.Drawing.Common only supported on Windows.

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. To perform a bit-block transfer of color data from the screen to the drawing surface of the Graphics object, see CopyFromScreen. For more information about how to draw images with a Graphics object, see Working with Images, Bitmaps, Icons, and Metafiles.

In addition, you can manipulate the coordinate system used by the Graphics object. For more information on the coordinate system and how to manipulate it, see Coordinate Systems and Transformations.

Properties

Name Description
Clip

Gets or sets a Region that limits the drawing region of this Graphics.

ClipBounds

Gets a RectangleF structure that bounds the clipping region of this Graphics.

CompositingMode

Gets a value that specifies how composited images are drawn to this Graphics.

CompositingQuality

Gets or sets the rendering quality of composited images drawn to this Graphics.

DpiX

Gets the horizontal resolution of this Graphics.

DpiY

Gets the vertical resolution of this Graphics.

InterpolationMode

Gets or sets the interpolation mode associated with this Graphics.

IsClipEmpty

Gets a value indicating whether the clipping region of this Graphics is empty.

IsVisibleClipEmpty

Gets a value indicating whether the visible clipping region of this Graphics is empty.

PageScale

Gets or sets the scaling between world units and page units for this Graphics.

PageUnit

Gets or sets the unit of measure used for page coordinates in this Graphics.

PixelOffsetMode

Gets or sets a value specifying how pixels are offset during rendering of this Graphics.

RenderingOrigin

Gets or sets the rendering origin of this Graphics for dithering and for hatch brushes.

SmoothingMode

Gets or sets the rendering quality for this Graphics.

TextContrast

Gets or sets the gamma correction value for rendering text.

TextRenderingHint

Gets or sets the rendering mode for text associated with this Graphics.

Transform

Gets or sets a copy of the geometric world transformation for this Graphics.

TransformElements

Gets or sets the world transform elements for this Graphics.

VisibleClipBounds

Gets the bounding rectangle of the visible clipping region of this Graphics.

Methods

Name Description
AddMetafileComment(Byte[])

Adds a comment to the current Metafile.

BeginContainer()

Saves a graphics container with the current state of this Graphics and opens and uses a new graphics container.

BeginContainer(Rectangle, Rectangle, GraphicsUnit)

Saves a graphics container with the current state of this Graphics and opens and uses a new graphics container with the specified scale transformation.

BeginContainer(RectangleF, RectangleF, GraphicsUnit)

Saves a graphics container with the current state of this Graphics and opens and uses a new graphics container with the specified scale transformation.

Clear(Color)

Clears the entire drawing surface and fills it with the specified background color.

CopyFromScreen(Int32, Int32, Int32, Int32, Size, CopyPixelOperation)

Performs a bit-block transfer of the color data, corresponding to a rectangle of pixels, from the screen to the drawing surface of the Graphics.

CopyFromScreen(Int32, Int32, Int32, Int32, Size)

Performs a bit-block transfer of the color data, corresponding to a rectangle of pixels, from the screen to the drawing surface of the Graphics.

CopyFromScreen(Point, Point, Size, CopyPixelOperation)

Performs a bit-block transfer of color data, corresponding to a rectangle of pixels, from the screen to the drawing surface of the Graphics.

CopyFromScreen(Point, Point, Size)

Performs a bit-block transfer of color data, corresponding to a rectangle of pixels, from the screen to the drawing surface of the Graphics.

CreateObjRef(Type)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(Inherited from MarshalByRefObject)
Dispose()

Releases all resources used by this Graphics.

DrawArc(Pen, Int32, Int32, Int32, Int32, Int32, Int32)

Draws an arc representing a portion of an ellipse specified by a pair of coordinates, a width, and a height.

DrawArc(Pen, Rectangle, Single, Single)

Draws an arc representing a portion of an ellipse specified by a Rectangle structure.

DrawArc(Pen, RectangleF, Single, Single)

Draws an arc representing a portion of an ellipse specified by a RectangleF structure.

DrawArc(Pen, Single, Single, Single, Single, Single, Single)

Draws an arc representing a portion of an ellipse specified by a pair of coordinates, a width, and a height.

DrawBezier(Pen, Point, Point, Point, Point)

Draws a Bézier spline defined by four Point structures.

DrawBezier(Pen, PointF, PointF, PointF, PointF)

Draws a Bézier spline defined by four PointF structures.

DrawBezier(Pen, Single, Single, Single, Single, Single, Single, Single, Single)

Draws a Bézier spline defined by four ordered pairs of coordinates that represent points.

DrawBeziers(Pen, Point[])

Draws a series of Bézier splines from an array of Point structures.

DrawBeziers(Pen, PointF[])

Draws a series of Bézier splines from an array of PointF structures.

DrawBeziers(Pen, ReadOnlySpan<Point>)

Draws a series of Bézier splines from an array of Point structures.

DrawBeziers(Pen, ReadOnlySpan<PointF>)

Draws a series of Bézier splines from an array of Point structures.

DrawCachedBitmap(CachedBitmap, Int32, Int32)

Draws the given cachedBitmap.

DrawClosedCurve(Pen, Point[], Single, FillMode)

Draws a closed cardinal spline defined by an array of Point structures using a specified tension.

DrawClosedCurve(Pen, Point[])

Draws a closed cardinal spline defined by an array of Point structures.

DrawClosedCurve(Pen, PointF[], Single, FillMode)

Draws a closed cardinal spline defined by an array of PointF structures using a specified tension.

DrawClosedCurve(Pen, PointF[])

Draws a closed cardinal spline defined by an array of PointF structures.

DrawClosedCurve(Pen, ReadOnlySpan<Point>, Single, FillMode)

Draws a closed cardinal spline defined by an array of PointF structures using a specified tension.

DrawClosedCurve(Pen, ReadOnlySpan<Point>)

Draws a closed cardinal spline defined by an array of PointF structures using a specified tension.

DrawClosedCurve(Pen, ReadOnlySpan<PointF>, Single, FillMode)

Draws a closed cardinal spline defined by an array of PointF structures using a specified tension.

DrawClosedCurve(Pen, ReadOnlySpan<PointF>)

Draws a closed cardinal spline defined by an array of PointF structures using a specified tension.

DrawCurve(Pen, Point[], Int32, Int32, Single)

Draws a cardinal spline through a specified array of Point structures using a specified tension.

DrawCurve(Pen, Point[], Single)

Draws a cardinal spline through a specified array of Point structures using a specified tension.

DrawCurve(Pen, Point[])

Draws a cardinal spline through a specified array of Point structures.

DrawCurve(Pen, PointF[], Int32, Int32, Single)

Draws a cardinal spline through a specified array of PointF structures using a specified tension. The drawing begins offset from the beginning of the array.

DrawCurve(Pen, PointF[], Int32, Int32)

Draws a cardinal spline through a specified array of PointF structures. The drawing begins offset from the beginning of the array.

DrawCurve(Pen, PointF[], Single)

Draws a cardinal spline through a specified array of PointF structures using a specified tension.

DrawCurve(Pen, PointF[])

Draws a cardinal spline through a specified array of PointF structures.

DrawCurve(Pen, ReadOnlySpan<Point>, Int32, Int32, Single)

Draws a cardinal spline through a specified array of Point structures using a specified tension.

DrawCurve(Pen, ReadOnlySpan<Point>, Single)

Draws a cardinal spline through a specified array of Point structures using a specified tension.

DrawCurve(Pen, ReadOnlySpan<Point>)

Draws a cardinal spline through a specified array of Point structures using a specified tension.

DrawCurve(Pen, ReadOnlySpan<PointF>, Int32, Int32, Single)

Draws a cardinal spline through a specified array of Point structures using a specified tension.

DrawCurve(Pen, ReadOnlySpan<PointF>, Int32, Int32)

Draws a cardinal spline through a specified array of Point structures using a specified tension.

DrawCurve(Pen, ReadOnlySpan<PointF>, Single)

Draws a cardinal spline through a specified array of Point structures using a specified tension.

DrawCurve(Pen, ReadOnlySpan<PointF>)

Draws a cardinal spline through a specified array of Point structures using a specified tension.

DrawEllipse(Pen, Int32, Int32, Int32, Int32)

Draws an ellipse defined by a bounding rectangle specified by coordinates for the upper-left corner of the rectangle, a height, and a width.

DrawEllipse(Pen, Rectangle)

Draws an ellipse specified by a bounding Rectangle structure.

DrawEllipse(Pen, RectangleF)

Draws an ellipse defined by a bounding RectangleF.

DrawEllipse(Pen, Single, Single, Single, Single)

Draws an ellipse defined by a bounding rectangle specified by a pair of coordinates, a height, and a width.

DrawIcon(Icon, Int32, Int32)

Draws the image represented by the specified Icon at the specified coordinates.

DrawIcon(Icon, Rectangle)

Draws the image represented by the specified Icon within the area specified by a Rectangle structure.

DrawIconUnstretched(Icon, Rectangle)

Draws the image represented by the specified Icon without scaling the image.

DrawImage(Image, Effect, RectangleF, Matrix, GraphicsUnit, ImageAttributes)

Draws a portion of an image after applying a specified effect.

DrawImage(Image, Effect)

Draws a portion of an image after applying a specified effect.

DrawImage(Image, Int32, Int32, Int32, Int32)

Draws the specified Image at the specified location and with the specified size.

DrawImage(Image, Int32, Int32, Rectangle, GraphicsUnit)

Draws a portion of an image at a specified location.

DrawImage(Image, Int32, Int32)

Draws the specified image, using its original physical size, at the location specified by a coordinate pair.

DrawImage(Image, Point)

Draws the specified Image, using its original physical size, at the specified location.

DrawImage(Image, Point[], Rectangle, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, Int32)

Draws the specified portion of the specified Image at the specified location and with the specified size.

DrawImage(Image, Point[], Rectangle, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort)

Draws the specified portion of the specified Image at the specified location and with the specified size.

DrawImage(Image, Point[], Rectangle, GraphicsUnit, ImageAttributes)

Draws the specified portion of the specified Image at the specified location.

DrawImage(Image, Point[], Rectangle, GraphicsUnit)

Draws the specified portion of the specified Image at the specified location and with the specified size.

DrawImage(Image, Point[])

Draws the specified Image at the specified location and with the specified shape and size.

DrawImage(Image, PointF)

Draws the specified Image, using its original physical size, at the specified location.

DrawImage(Image, PointF[], RectangleF, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, Int32)

Draws the specified portion of the specified Image at the specified location and with the specified size.

DrawImage(Image, PointF[], RectangleF, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort)

Draws the specified portion of the specified Image at the specified location and with the specified size.

DrawImage(Image, PointF[], RectangleF, GraphicsUnit, ImageAttributes)

Draws the specified portion of the specified Image at the specified location and with the specified size.

DrawImage(Image, PointF[], RectangleF, GraphicsUnit)

Draws the specified portion of the specified Image at the specified location and with the specified size.

DrawImage(Image, PointF[])

Draws the specified Image at the specified location and with the specified shape and size.

DrawImage(Image, Rectangle, Int32, Int32, Int32, Int32, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, IntPtr)

Draws the specified portion of the specified Image at the specified location and with the specified size.

DrawImage(Image, Rectangle, Int32, Int32, Int32, Int32, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort)

Draws the specified portion of the specified Image at the specified location and with the specified size.

DrawImage(Image, Rectangle, Int32, Int32, Int32, Int32, GraphicsUnit, ImageAttributes)

Draws the specified portion of the specified Image at the specified location and with the specified size.

DrawImage(Image, Rectangle, Int32, Int32, Int32, Int32, GraphicsUnit)

Draws the specified portion of the specified Image at the specified location and with the specified size.

DrawImage(Image, Rectangle, Rectangle, GraphicsUnit)

Draws the specified portion of the specified Image at the specified location and with the specified size.

DrawImage(Image, Rectangle, Single, Single, Single, Single, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, IntPtr)

Draws the specified portion of the specified Image at the specified location and with the specified size.

DrawImage(Image, Rectangle, Single, Single, Single, Single, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort)

Draws the specified portion of the specified Image at the specified location and with the specified size.

DrawImage(Image, Rectangle, Single, Single, Single, Single, GraphicsUnit, ImageAttributes)

Draws the specified portion of the specified Image at the specified location and with the specified size.

DrawImage(Image, Rectangle, Single, Single, Single, Single, GraphicsUnit)

Draws the specified portion of the specified Image at the specified location and with the specified size.

DrawImage(Image, Rectangle)

Draws the specified Image at the specified location and with the specified size.

DrawImage(Image, RectangleF, RectangleF, GraphicsUnit)

Draws the specified portion of the specified Image at the specified location and with the specified size.

DrawImage(Image, RectangleF)

Draws the specified Image at the specified location and with the specified size.

DrawImage(Image, Single, Single, RectangleF, GraphicsUnit)

Draws a portion of an image at a specified location.

DrawImage(Image, Single, Single, Single, Single)

Draws the specified Image at the specified location and with the specified size.

DrawImage(Image, Single, Single)

Draws the specified Image, using its original physical size, at the specified location.

DrawImageUnscaled(Image, Int32, Int32, Int32, Int32)

Draws a specified image using its original physical size at a specified location.