GDI+ graphics

• GDI stands for Graphic Device Interface.
• This concept is used to create user defined graphical elements in the form, like lines, rectangles, circles, triangles etc.
• This is similar to “graphics” concept in C.
API: System.Drawings.Graphics
This class object is able to write any graphics on it’s container. That means every graphic object requires a container. The container may be either form or panel.

Note: The GDI graphics can be implemented in “Paint()” method.

Implementation: private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = this.CreateGraphics();
}

In the above code, the “CreateGraphics()” method creates a graphics object, that is able to write the GDI graphics in the container (form).

Drawing the GDI Graphics

1) Drawing Lines
 g.DrawLine(Pens.xxxx, x1, y1, x2, y2);

2) Drawing Rectangles
 g.DrawRectangle(Pens.xxxx, x1, y1, width, height);

3) Drawing Circles / Elipses
 g.DrawEllipse(Pens.xxxx, x1, y1, width, height);

4) Drawing Polygons
 Point[] p = new Point[count];
p[0] = new Point(x,y);
p[1] = new Point(x,y);
p[2] = new Point(x,y);
……………………..
g.DrawPolygon(Pens.xxx, p);

5) Drawing Curves
 Point[] p = new Point[count];
p[0] = new Point(x,y);
p[1] = new Point(x,y);
p[2] = new Point(x,y);
……………………..
g.DrawCurve(Pens.xxx, p);

Demo on GDI (Drawing Lines)

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = this.CreateGraphics();
g.DrawLine(Pens.RoyalBlue, 100, 100, 300, 100);
g.DrawLine(Pens.Red, 150, 150, 300, 150);
g.DrawLine(Pens.SandyBrown, 200, 200, 300, 200);
g.DrawLine(Pens.SlateGray, 250, 250, 300, 250);
}

Demo on GDI (Drawing Lines)

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = this.CreateGraphics();
int h = this.Size.Height - 35;
int w = this.Size.Width - 9;
g.DrawLine(Pens.Coral, 0, 0, w, h);
g.DrawLine(Pens.Coral, w, 0, 0, h);
}

Demo on GDI (Drawing Lines)

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = this.CreateGraphics();
int w = this.Size.Width - 9;
int h = this.Size.Height - 35;
for (int i = 0; i <= h; i += 4)
g.DrawLine(Pens.DarkGreen, 0, i, w, i);
for (int i = 0; i <= w; i += 4)
g.DrawLine(Pens.Red, i, 0, i, h);
}

Demo on GDI (Drawing Lines)

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
Graphics g = this.CreateGraphics();
g.DrawLine(Pens.Orange, 0, 0, e.X, e.Y);
}

Demo on GDI (Drawing Lines, Rectangles, Circles, Triangles, Curves)

private void Form1_Paint(object sender, PaintEventArgs e)
{
this.Size = new Size(409, 435);
Graphics g = this.CreateGraphics();
//dividing lines
g.DrawLine(Pens.Green, 200, 0, 200, 400);
g.DrawLine(Pens.Green, 0, 200, 400, 200);
//rectangle
g.DrawRectangle(Pens.Red, 50, 50, 100, 100);
//circle or ellipse
g.DrawEllipse(Pens.Red, 250, 50, 100, 100);
//triangle (with polygon)
Point[] p1 = new Point[3];
p1[0] = new Point(100, 250);
p1[1] = new Point(50, 350);
p1[2] = new Point(150, 350);
g.DrawPolygon(Pens.Red, p1);
//curve
Point[] p2 = new Point[6];
p2[0] = new Point(250, 350);
p2[1] = new Point(250, 250);
p2[2] = new Point(350, 250);
p2[3] = new Point(350, 350);
p2[4] = new Point(300, 300);
p2[5] = new Point(250, 350);
g.DrawCurve(Pens.Red, p2);
}

Demo on GDI (Drawing Circles)

private void Form1_MouseClick(object sender, MouseEventArgs e)
{
Graphics g = this.CreateGraphics();
int x = e.X;
int y = e.Y;
g.DrawEllipse(Pens.Brown, x-20, y-20, 40, 40);
}

Demo on GDI (Drawing Filled Objects)

private void Form1_Paint(object sender, PaintEventArgs e)
{
this.Size = new Size(409, 435);
Graphics g = this.CreateGraphics();
//dividing lines
g.DrawLine(Pens.Green, 200, 0, 200, 400);
g.DrawLine(Pens.Green, 0, 200, 400, 200);
//rectangle
g.FillRectangle(Brushes.Violet, 50, 50, 100, 100);
//circle or ellipse
g.FillEllipse(Brushes.Salmon, 250, 50, 100, 100);
//triangle (with polygon)
Point[] p1 = new Point[3];
p1[0] = new Point(100, 250);
p1[1] = new Point(50, 350);
p1[2] = new Point(150, 350);
g.FillPolygon(Brushes.Chocolate, p1);
//curve
Point[] p2 = new Point[5];
p2[0] = new Point(250, 350);
p2[1] = new Point(250, 250);
p2[2] = new Point(350, 250);
p2[3] = new Point(350, 350);

p2[4] = new Point(300, 300);
g.FillClosedCurve(Brushes.LightSeaGreen, p2);
}

Demo on GDI (Drawing Filled Objects)

private void Form1_Paint(object sender, PaintEventArgs e)
{
this.Size = new Size(509, 535);
Graphics g = this.CreateGraphics();
g.FillRectangle(Brushes.Pink, 0, 0, 100, 500);
g.FillRectangle(Brushes.Purple, 101, 0, 100, 500);
g.FillRectangle(Brushes.LightGreen, 201, 0, 100, 500);
g.FillRectangle(Brushes.Linen, 301, 0, 100, 500);
g.FillRectangle(Brushes.MediumTurquoise, 401, 0, 100, 500);
}