.NET Framework Docs

Graphics Class

The System.Drawing.Graphics class provides methods for drawing objects to a display device. It is the primary class for rendering shapes, images, and text in Windows Forms applications.

Key Features

Getting a Graphics Object

// Within a Form or Control
protected override void OnPaint(PaintEventArgs e)
{
  Graphics g = e.Graphics;
  // Use g to draw
  base.OnPaint(e);
}

Common Methods

MethodDescription
DrawLineDraws a line between two points.
DrawRectangleDraws the outline of a rectangle.
FillRectangleFills the interior of a rectangle.
DrawEllipseDraws the outline of an ellipse.
FillEllipseFills the interior of an ellipse.
DrawStringRenders text using a specified font and brush.
MeasureStringMeasures the size of a string before drawing.
DrawImageDraws an image to the graphics surface.
TransformApplies a transformation matrix to subsequent drawing operations.
ResetTransformResets the world transformation to the identity matrix.

Examples

Below is a simple example that draws a red circle and some text.

protected override void OnPaint(PaintEventArgs e)
{
  Graphics g = e.Graphics;
  using (Pen redPen = new Pen(Color.Red, 2))
  {
    g.DrawEllipse(redPen, 50, 50, 200, 200);
  }
  using (Brush brush = new SolidBrush(Color.Blue))
  {
    g.DrawString("Hello, Graphics!", new Font("Segoe UI", 16), brush, new PointF(80, 260));
  }
  base.OnPaint(e);
}

Further Reading