Graphics.DrawLine Method

Definition

Draws a line connecting the two points specified by the coordinate pairs.

Overloads

DrawLine(Pen, Int32, Int32, Int32, Int32)

Draws a line connecting the two points specified by the coordinate pairs.

DrawLine(Pen, Single, Single, Single, Single)

Draws a line connecting the two points specified by the coordinate pairs.

DrawLine(Pen, Point, Point)

Draws a line connecting two Point structures.

DrawLine(Pen, PointF, PointF)

Draws a line connecting two PointF structures.

DrawLine(Pen, Int32, Int32, Int32, Int32)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

Draws a line connecting the two points specified by the coordinate pairs.

public void DrawLine (System.Drawing.Pen pen, int x1, int y1, int x2, int y2);

Parameters

pen
Pen

Pen that determines the color, width, and style of the line.

x1
Int32

The x-coordinate of the first point.

y1
Int32

The y-coordinate of the first point.

x2
Int32

The x-coordinate of the second point.

y2
Int32

The y-coordinate of the second point.

Exceptions

pen is null.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates a black pen.

  • Creates the coordinates of the endpoints of the line.

  • Draws the line to the screen.

public void DrawLineInt(PaintEventArgs e)
{
             
    // Create pen.
    Pen blackPen = new Pen(Color.Black, 3);
             
    // Create coordinates of points that define line.
    int x1 = 100;
    int y1 = 100;
    int x2 = 500;
    int y2 = 100;
             
    // Draw line to screen.
    e.Graphics.DrawLine(blackPen, x1, y1, x2, y2);
}

Remarks

This method draws a line connecting the two points specified by the x1, y1, x2, and y2 parameters.

See also

Applies to

.NET 9 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawLine(Pen, Single, Single, Single, Single)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

Draws a line connecting the two points specified by the coordinate pairs.

public void DrawLine (System.Drawing.Pen pen, float x1, float y1, float x2, float y2);

Parameters

pen
Pen

Pen that determines the color, width, and style of the line.

x1
Single

The x-coordinate of the first point.

y1
Single

The y-coordinate of the first point.

x2
Single

The x-coordinate of the second point.

y2
Single

The y-coordinate of the second point.

Exceptions

pen is null.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates a black pen.

  • Creates the coordinates of the endpoints of the line.

  • Draws the line to the screen.

public void DrawLineFloat(PaintEventArgs e)
{
             
    // Create pen.
    Pen blackPen = new Pen(Color.Black, 3);
             
    // Create coordinates of points that define line.
    float x1 = 100.0F;
    float y1 = 100.0F;
    float x2 = 500.0F;
    float y2 = 100.0F;
             
    // Draw line to screen.
    e.Graphics.DrawLine(blackPen, x1, y1, x2, y2);
}

Remarks

This method draws a line connecting the two points specified by the x1, y1, x2, and y2 parameters.

See also

Applies to

.NET 9 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawLine(Pen, Point, Point)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

Draws a line connecting two Point structures.

public void DrawLine (System.Drawing.Pen pen, System.Drawing.Point pt1, System.Drawing.Point pt2);

Parameters

pen
Pen

Pen that determines the color, width, and style of the line.

pt1
Point

Point structure that represents the first point to connect.

pt2
Point

Point structure that represents the second point to connect.

Exceptions

pen is null.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates a black pen.

  • Creates points for the endpoints of the line.

  • Draws the line to the screen.

public void DrawLinePoint(PaintEventArgs e)
{
             
    // Create pen.
    Pen blackPen = new Pen(Color.Black, 3);
             
    // Create points that define line.
    Point point1 = new Point(100, 100);
    Point point2 = new Point(500, 100);
             
    // Draw line to screen.
    e.Graphics.DrawLine(blackPen, point1, point2);
}

See also

Applies to

.NET 9 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawLine(Pen, PointF, PointF)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

Draws a line connecting two PointF structures.

public void DrawLine (System.Drawing.Pen pen, System.Drawing.PointF pt1, System.Drawing.PointF pt2);

Parameters

pen
Pen

Pen that determines the color, width, and style of the line.

pt1
PointF

PointF structure that represents the first point to connect.

pt2
PointF

PointF structure that represents the second point to connect.

Exceptions

pen is null.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates a black pen.

  • Creates points for the endpoints of the line.

  • Draws the line to the screen.

public void DrawLinePointF(PaintEventArgs e)
{
             
    // Create pen.
    Pen blackPen = new Pen(Color.Black, 3);
             
    // Create points that define line.
    PointF point1 = new PointF(100.0F, 100.0F);
    PointF point2 = new PointF(500.0F, 100.0F);
             
    // Draw line to screen.
    e.Graphics.DrawLine(blackPen, point1, point2);
}

Remarks

This method draws a line connecting the two points specified by the pt1 and p2 parameters.

See also

Applies to

.NET 9 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9