C# | Graphics.DrawLine() Method | Set – 2
Last Updated :
01 May, 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
First, two methods are already discussed in Set – 1. Here, we will discuss the last two methods.
DrawLine(Pen, Single, Single, Single, Single) 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, float x1, float y1, float x2, float 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:
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, 30.0F, 30.0F, 200.0f, 300.0f);
}
}
}
|
Output:
DrawLine(Pen, Point, Point)
This method is used to draw a line from a specified set of points to a specified set of points. It needs a Point 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 Point variable of the initial point.
- pt2: Defines the (x, y) coordinates as a Point 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);
Point pt1 = new Point(30, 30);
Point pt2 = new Point(200, 300);
pea.Graphics.DrawLine(pen, pt1, pt2);
}
}
}
|
Output:
Share your thoughts in the comments
Please Login to comment...