Draw Line in C#
In this article I will explain you how to draw lines in GDI+.
This article has been excerpted from book "Graphics Programming with GDI+ ".
The DrawLine method draws a line between two points specified by a pair of coordinates. DrawLines draws a series of lines using an array of points.
DrawLine has four overloaded methods. The first argument of all DrawLine methods is a Pen object, with texture, color, and width attributes. The rest of the arguments vary. You can use two points with integer or floating point values, or you can pass four integer or floating point values directly:
public void DrawLine (Pen, Point, Point);
public void DrawLine (Pen, PointF, PointF);
public void DrawLine (Pen, int, int, int, int);
public void DrawLine (Pen, float, float, float, float);
To draw a line, an application first creates a Pen object, that defines the color and width. The following line of code creates a red pen with a width of 1:
Pen redPen = new Pen(Color.Red, 1);
After that we define the endpoints of the line:
float x1 = 20.0F, y1 = 25.0F;
float x2 = 200.0F, y2 = 100.0F;
Finally, we use the pen and points as input to DrawLine:
Graphics.DrawLine(redPen, x1, y1, x2, y2);
Listing 3.1 shows how to use the various overloaded methods. We create four pens with different colors and widths. After that we call DrawLine with various values, including integer, floating point, and Point structures, to draw four different lines. Three of them start at point (20,20).
LISTING 3.1: Drawing lines
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
// Create four Pen objects with red,
// blue, green, and black colors and
// different widths
Pen redPen = new Pen(Color.Red, 1);
Pen bluePen = new Pen(Color.Blue, 2);
Pen greenPen = new Pen(Color.Green, 3);
Pen blackPen = new Pen(Color.Black, 4);
// Draw line using float coordinates
float x1 = 20.0F, y1 = 20.0F;
float x2 = 200.0F, y2 = 20.0F;
e.Graphics.DrawLine(redPen, x1, y1, x2, y2);
// Draw line using Point structure
Point p1 = new Point(20, 20);
Point p2 = new Point(20, 200);
e.Graphics.DrawLine(greenPen, p1, p2);
//Draw line using PointF structures
PointF ptf1 = new PointF(20.0F, 20.0f);
PointF ptf2 = new PointF(200.0F, 200.0f);
e.Graphics.DrawLine(bluePen, ptf1, ptf2);
// Draw line using integer coordinates
int X1 = 60, Y1 = 40, X2 = 250, Y2 = 100;
e.Graphics.DrawLine(blackPen, X1, Y1, X2, Y2);
//Dispose of objects
The output from listing 3.1 is shown in Figure 3.1. We've drawn four lines starting at point (20,20).
Drawing Connected Lines
Sometimes we need to draw multiple connected straight line segments. One way to do this is to call the DrawLine method multiples times.
FIGURE 3.1: Using DrawLine to draw lines
The Graphics class also provides the DrawLine method, that can be used to draw multiple connected lines. This method has two overloaded forms. One takes an array of Point structure objects, and the other takes an array of PointF structure objects:
public void DrawLine (Pen, Point );
public void DrawLine (Pen, PointF );
To draw lines using DrawLines, an application first creates a Pen object, then creates an array of points, and then calls DrawLines. The code in Listing 3.2 draws three line segments.
LISTING 3.2: Using DrawLines to draw connected lines
new PointF(20.0F, 20.0F),
new PointF (20.0F, 20.0F),
new PointF (200.0F, 200.0F),
new PointF (20.0F, 20.0F)
e.Graphics.DrawLines (redPen, ptsArray);
The code in Listing 3.2 draws what is shown in Figure 3.2.
FIGURE 3.2: Using DrawLines to draw connected lines
I hope this article has helped you understand drawing lines. Read other articles on GDI+ on the website.
| ||This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows.|