C# | Graphics.DrawLine() Method | Set – 1
Last Updated :
17 Apr, 2019
Graphics.DrawLine() Method is used to draw a line connecting the two points specified by the coordinate pairs. There are 4 methods in the overload list of this method as follows:
- DrawLine(Pen, PointF, PointF) Method
- DrawLine(Pen, Int32, Int32, Int32, Int32) Method
- DrawLine(Pen, Single, Single, Single, Single) Method
- DrawLine(Pen, Point, Point) Method
Here, we will discuss the first two methods.
DrawLine(Pen, PointF, PointF) Method
This method is used to draw line form a specified set of points to a specified set of points. It needs a PointF variable which is set of (x, y) points.
Syntax:
public void DrawLine (System.Drawing.Pen pen, System.Drawing.PointF pt1, System.Drawing.PointF pt2);
Parameters:
- pen: Pen determines the color, width, and style of the line.
- pt1: Defines the (x, y) coordinates as a PointF variable of the initial point.
- pt2: Defines the (x, y) coordinates as a PointF variable of the final point.
Exception: This method will give ArgumentNullexception if the pen is null.
Example:
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace GFG {
class PrintableForm : Form {
public static void Main()
{
Application.Run( new PrintableForm());
}
public PrintableForm()
{
ResizeRedraw = true ;
}
protected override void OnPaint(PaintEventArgs pea)
{
Pen pen = new Pen(ForeColor);
PointF pt1 = new PointF(30.0F, 30.0F);
PointF pt2 = new PointF(200.0F, 300.0F);
pea.Graphics.DrawLine(pen, pt1, pt2);
}
}
}
|
Output:
DrawLine(Pen, Int32, Int32, Int32, Int32) Method
This method is used to draw line form a specified set of coordinates given in form x1, y1, x2, y2 all discrete.
Syntax:
public void DrawLine (System.Drawing.Pen pen, int x1, int y1, int x2, int y2);
Parameters:
- pen: Pen determines the color, width, and style of the line.
- x1: The abscissa of first point.
- y1: The ordinate of first point.
- x2: The abscissa of second point.
- y2: The ordinate of second point.
Exception: This method will give ArgumentNullexception if the pen is null.
Example 1:
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace GFG {
class PrintableForm : Form {
public static void Main()
{
Application.Run( new PrintableForm());
}
public PrintableForm()
{
ResizeRedraw = true ;
}
protected override void OnPaint(PaintEventArgs pea)
{
Pen pen = new Pen(ForeColor);
pea.Graphics.DrawLine(pen, 0, 0, ClientSize.Width - 1,
ClientSize.Height - 1);
pea.Graphics.DrawLine(pen, ClientSize.Width - 1, 0, 0,
ClientSize.Height - 1);
}
}
}
|
Output:
Example 2:
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace GFG {
class PrintableForm : Form {
public static void Main()
{
Application.Run( new PrintableForm());
}
public PrintableForm()
{
ResizeRedraw = true ;
}
protected override void OnPaint(PaintEventArgs pea)
{
Pen pen = new Pen(ForeColor);
for ( int i = 0; i < 7; i++)
{
pea.Graphics.DrawLine(pen, i * 30, 0, i * 30, 240);
}
for ( int i = 0; i < 7; i++)
{
pea.Graphics.DrawLine(pen, 0, i * 30,
240, i * 30);
}
}
}
}
|
Output:
Share your thoughts in the comments
Please Login to comment...