Using the Graphics Object in WinForms
In WinForms, drawing is done using the Graphics object.
💡 The Graphics object represents a drawing surface and provides methods to
draw shapes, text, and images.
How to Get a Graphics Object?
There are three ways to create a Graphics object:
1️Using e.Graphics in the OnPaint method (Recommended).
2️Calling this.CreateGraphics() (Not recommended for persistent
drawing).
3️Using a Bitmap and Graphics.FromImage() (For off-screen drawing).
Example: Drawing a Simple Line on a Form
Let's create a WinForms application and draw a line using
Graphics.DrawLine().
Step 1: Create a WinForms Application
1. Open Visual Studio.
2. Create a new Windows Forms App (.NET Framework) project.
3. Name it WinFormsGraphicsLesson1.
4. Open the Form1.cs file.
Example: Drawing a Line in C# using GDI+
using System;
using System.Drawing;
using System.Windows.Forms;
public class MyForm : Form
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics; // Get the Graphics object
Pen pen = new Pen(Color.Blue, 2); // Create a blue pen with width 2
g.DrawLine(pen, 10, 10, 200, 200); // Draw a diagonal line
pen.dispose();
Explanation of Code:
✔ OnPaint(PaintEventArgs e) → This method handles drawing when the
form is repainted.
✔ e.Graphics → Gets the Graphics object associated with the form.
✔ Pen(Color.Red, 3) → Creates a red pen with a thickness of 3.
✔ g.DrawLine(pen, 50, 50, 200, 200) → Draws a line from (50,50) to
(200,200).
✔ pen.Dispose() → Releases system resources used by the pen.
Draw a Triangle
Objective: Modify the program to draw a triangle using three lines.
Hint: Use DrawLine() to connect three points.
Pen pen = new Pen(Color.Blue, 3);
g.DrawLine(pen, 100, 50, 50, 150); // Line 1
g.DrawLine(pen, 50, 150, 150, 150); // Line 2
g.DrawLine(pen, 150, 150, 100, 50); // Line 3
pen.Dispose();
Draw a Rectangle and Fill It with Color
Objective: Use DrawRectangle() and FillRectangle().
Pen pen = new Pen(Color.Green, 3);
Brush brush = new SolidBrush(Color.Yellow);
g.DrawRectangle(pen, 50, 50, 150, 100);
g.FillRectangle(brush, 50, 50, 150, 100);
pen.Dispose();
brush.Dispose();
Draw a Circle (Ellipse)
Objective: Use DrawEllipse() to create a circle.
Pen pen = new Pen(Color.Red, 3);
g.DrawEllipse(pen, 50, 50, 100, 100); // Draws a circle
pen.Dispose();
Draw a Smiley Face
Objective: Use DrawEllipse(), FillEllipse(), and DrawArc().
// Face
Brush faceBrush = new SolidBrush(Color.Yellow);
g.FillEllipse(faceBrush, 50, 50, 100, 100);
// Eyes
Brush eyeBrush = new SolidBrush(Color.Black);
g.FillEllipse(eyeBrush, 70, 80, 10, 10);
g.FillEllipse(eyeBrush, 120, 80, 10, 10);
// Smile
Pen smilePen = new Pen(Color.Black, 3);
g.DrawArc(smilePen, 70, 90, 60, 40, 0, -180);
Draw a Digital Clock
Objective: Display the current time using DrawString().
string time = DateTime.Now.ToString("hh:mm:ss tt");
Font font = new Font("Arial", 16, FontStyle.Bold);
Brush brush = new SolidBrush(Color.Black);
g.DrawString(time, font, brush, 50, 50);
Draw a Moving Ball (Basic Animation)
Objective: Use Timer to move a ball across the screen.
//write this code in constructor
int x = 50;
Timer timer = new Timer { Interval = 50 };
timer.Tick += (s, e) => { x += 5; this.Invalidate(); };
timer.Start();
protected override void OnPaint(PaintEventArgs e)
Graphics g = e.Graphics;
Brush brush = new SolidBrush(Color.Blue);
g.FillEllipse(brush, x, 100, 50, 50);
Create a Simple Paint App
Objective: Allow users to draw with the mouse (MouseDown, MouseMove).
bool drawing = false;
Point lastPoint;
this.MouseDown += (s, e) => { drawing = true; lastPoint = e.Location; };
this.MouseUp += (s, e) => { drawing = false; };
this.MouseMove += (s, e) =>
if (drawing)
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.Black, 2);
g.DrawLine(pen, lastPoint, e.Location);
lastPoint = e.Location;
};