Sample Examples:

Drawing a rectangle
 
You can override OnPaint event of your form to draw an rectangle. The LinearGradientBrush encapsulates a brush and linear gradient.
 
protected override void OnPaint(PaintEventArgs pe) 
{ 
 	Graphics g = pe.Graphics ; 
 	Rectangle rect = new Rectangle(50, 30, 100, 100); 
 	LinearGradientBrush lBrush = new LinearGradientBr
ush(rect, Color.Red, Color.Yellow,
 LinearGradientMode.BackwardDiagonal); 
 	g.FillRectangle(lBrush, rect); 
} 
 
The output of the above code looks like the Figure below:
 
img
 
Figure: Drawing a rectangle.
 
Drawing an Arc
 
DrawArc function draws an arc. This function takes four arguments.
 
First is the Pen. You create a pen by using the Pen class. The Pen constructor takes at least one argument, the color or the brush of the pen. Second argument width of the pen or brush is optional.
 
Pen pn = new Pen( Color.Blue ); or Pen pn = new Pen( Color.Blue, 100 );
 
The second argument is a rectangle. You can create a rectangle by using Rectangle structure. The Rectangle constructor takes four int type arguments and they are left and right corners of the rectangle.
 
Rectangle rect = new Rectangle(50, 50, 200, 100); 
protected override void OnPaint(PaintEventArgs pe)
{
 	Graphics g = pe.Graphics ;
 	Pen pn = new Pen( Color.Blue );
 	Rectangle rect = new Rectangle(50, 50, 200, 100);
 	g.DrawArc( pn, rect, 12, 84 );
} 
 
The output looks like this:
 
img
 
Drawing a Line
 
DrawLine function of the Graphics class draws a line. It takes three parameters, a pen, and two Point class parameters, starting and ending points. Point class constructor takes x, y arguments.
 
protected override void OnPaint(PaintEventArgs pe) 
{ 
 	Graphics g = pe.Graphics ; 
 	Pen pn = new Pen( Color.Blue ); 
 	// Rectangle rect = new Rectangle(50, 50, 200, 100); 
 	Point pt1 = new Point( 30, 30); 
 	Point pt2 = new Point( 110, 100); 
 	g.DrawLine( pn, pt1, pt2 ); 
} 
 
The output looks like this:
 
img
 
Drawing an Ellipse
 
An ellipse( or a circle) can be drawn by using DrawEllipse method. This method takes only two parameters, Pen and rectangle.
 
protected override void OnPaint(PaintEventArgs pe) 
{ 
 	Graphics g = pe.Graphics ; 
 	Pen pn = new Pen( Color.Blue, 100 ); 
 	Rectangle rect = new Rectangle(50, 50, 200, 100); 
 	g.DrawEllipse( pn, rect ); 
} 
 
The output looks like this:
 
img
 
The FillPath
 
Drawing bazier curves is little more complex than other objects.
 
protected override void OnPaint(PaintEventArgs pe)
{
 	Graphics g = pe.Graphics;
 	g.FillRectangle(new SolidBrush(Color.White), ClientRectangle);
 	GraphicsPath path = new GraphicsPath
(new Point[] 
{
	new Point(40, 140), new Point(275, 200),
 	new Point(105, 225), new Point(190, 300),
 	new Point(50, 350), new Point(20, 180), 
},
new byte[]
 	{
 		(byte)PathPointType.Start,
 		(byte)PathPointType.Bezier,
 		(byte)PathPointType.Bezier,
 		(byte)PathPointType.Bezier,
 		(byte)PathPointType.Line,
 		(byte)PathPointType.Line,
 	});
 	PathGradientBrush pgb = new PathGradientBrush(path);
 	pgb.SurroundColors = new Color
[] {  Color.Green,Color.Yellow,Color.Red, Color.Blue,
Color.Orange, Color.White, };
g.FillPath(pgb, path);
} 
 
The output looks like this:
 
img
 
Drawing Text and Strings
 
You can override OnPaint event of your form to draw an rectangle. The LinearGradientBrush encapsulates a brush and linear gradient.
 
protected override void OnPaint(PaintEventArgs pe)
{
 	Font fnt = new Font("Verdana", 16);
 	Graphics g = pe.Graphics;
 	g.DrawString("GDI+ World", fnt, new SolidBrush(Color.Red), 14,10);
} 
 
The output looks like this:
 
img
 
To Download Graphical Device Interface example Click here.
 
To view downloaded examples, Please Ensure that .NET Framework is installed on your system.