This tutorial will show you how to draw various shapes in a Window Form Application using the Graphics namespace. C# version.
Creating graphics in .NET can be very easy. In this tutorial, you will learn how to make a Windows Form Application draw different shapes using built-in methods of System.Drawing.Graphics
Creating graphics in .NET can be very easy. In this tutorial, you will learn how to make a Windows Form Application draw different shapes using built-in methods of System.Drawing.Graphics
We will be creating a form with a PictureBox control and several buttons. These buttons will draw pre-defined shapes using all built-in methods. We will first design our form with one large Picture Box (size: 570, 300), and then add eight buttons to do the following: draw a line, ellipse, rectangle, arc, pie, polygon and bezier, and a button to clear the canvas.
Once we have these controls added to the form, we can begin adding the functionality. Double-click each button on the design view to create the click event handlers for them. First we will instantiate our graphics and pen, then draw a line:
0
1
2
3
4
5
6
7
8
9
|
private System.Drawing.Graphics g;
private System.Drawing.Pen pen1 = new System.Drawing.Pen(Color.Blue, 2F);
private void butLine_Click(object sender, EventArgs e)
{
g = PictureBox1.CreateGraphics();
g.DrawLine(pen1, 250, 50, 400, 200);
}
|
The usage of the majority of the methods are similar, so we assign our graphics variable to the CreateGraphics method on each button click and then use the graphics methods to create the shapes.
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
private void butEllipse_Click(object sender, EventArgs e)
{
g = PictureBox1.CreateGraphics();
g.DrawEllipse(pen1, 50, 50, 100, 150);
}
private void butRectangle_Click(object sender, EventArgs e)
{
g = PictureBox1.CreateGraphics();
g.DrawRectangle(pen1, 30, 30, 50, 60);
}
private void butArc_Click(object sender, EventArgs e)
{
g = PictureBox1.CreateGraphics();
g.DrawArc(pen1, 150, 100, 150, 200, 150, 160);
}
private void butPie_Click(object sender, EventArgs e)
{
g = PictureBox1.CreateGraphics();
g.DrawPie(pen1, 50, 50, 150, 150, 0, 170);
}
|
The Polygon is slightly different in that we are required to plot the points of each corner before we draw:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
private void butPolygon_Click(object sender, EventArgs e) {
System.Drawing.Point[] p = new System.Drawing.Point[6];
p[0].X = 0;
p[0].Y = 0;
p[1].X = 53;
p[1].Y = 111;
p[2].X = 114;
p[2].Y = 86;
p[3].X = 34;
p[3].Y = 34;
p[4].X = 165;
p[4].Y = 7;
g = PictureBox1.CreateGraphics();
g.DrawPolygon(pen1, p);
}
The bezier is similar in that we plot the points as well, but we can do it in fewer steps than the polygon. The values in the bezier are the x and y coordinates of the control points.
We can simply code the clear button by calling the Refresh method of the PictureBox:
private void butBezier_Click(object sender, EventArgs e)
{
g = PictureBox1.CreateGraphics();
g.DrawBezier(pen1, 100, 200, 240, 250, 100, 200, 150, 30);
}
private void butClear_Click(object sender, EventArgs e)
{
PictureBox1.Refresh();
}
|
The entire code-behind is as follows:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinForms_Drawing_cs
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private System.Drawing.Graphics g;
private System.Drawing.Pen pen1 = new System.Drawing.Pen(Color.Blue, 2F);
private void butLine_Click(object sender, EventArgs e)
{
g = PictureBox1.CreateGraphics();
g.DrawLine(pen1, 250, 50, 400, 200);
}
private void butEllipse_Click(object sender, EventArgs e)
{
g = PictureBox1.CreateGraphics();
g.DrawEllipse(pen1, 50, 50, 100, 150);
}
private void butRectangle_Click(object sender, EventArgs e)
{
g = PictureBox1.CreateGraphics();
g.DrawRectangle(pen1, 30, 30, 50, 60);
}
private void butArc_Click(object sender, EventArgs e)
{
g = PictureBox1.CreateGraphics();
g.DrawArc(pen1, 150, 100, 150, 200, 150, 160);
}
private void butPie_Click(object sender, EventArgs e)
{
g = PictureBox1.CreateGraphics();
g.DrawPie(pen1, 50, 50, 150, 150, 0, 170);
}
private void butPolygon_Click(object sender, EventArgs e)
{
System.Drawing.Point[] p = new System.Drawing.Point[6];
p[0].X = 0;
p[0].Y = 0;
p[1].X = 53;
p[1].Y = 111;
p[2].X = 114;
p[2].Y = 86;
p[3].X = 34;
p[3].Y = 34;
p[4].X = 165;
p[4].Y = 7;
g = PictureBox1.CreateGraphics();
g.DrawPolygon(pen1, p);
}
private void butBezier_Click(object sender, EventArgs e)
{
g = PictureBox1.CreateGraphics();
g.DrawBezier(pen1, 100, 200, 240, 250, 100, 200, 150, 30);
}
private void butClear_Click(object sender, EventArgs e)
{
PictureBox1.Refresh();
}
}
}
|
Now when we run this application, we will be able to click each button and see the different shapes appear on the form.
Comments
Post a Comment