Graphics.DrawLine 方法

定義

繪製連接座標組所指定兩個點的線條。

多載

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

繪製連接座標組所指定兩個點的線條。

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

繪製連接座標組所指定兩個點的線條。

DrawLine(Pen, Point, Point)

繪製連接兩個 Point 結構的線條。

DrawLine(Pen, PointF, PointF)

繪製連接兩個 PointF 結構的線條。

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

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

繪製連接座標組所指定兩個點的線條。

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

參數

pen
Pen

Pen,決定線條的色彩、寬度和樣式。

x1
Int32

第一個點的 X 座標。

y1
Int32

第一個點的 Y 座標。

x2
Int32

第二個點的 X 座標。

y2
Int32

第二個點的 Y 座標。

例外狀況

pen null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:

  • 建立黑色畫筆。

  • 建立線條端點的座標。

  • 將線條繪製到畫面。

C#
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);
}

備註

這個方法會繪製一條線,連接 x1y1x2y2 參數指定的兩個點。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET 6, 7, 8, 9
.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, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

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

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

繪製連接座標組所指定兩個點的線條。

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

參數

pen
Pen

Pen,決定線條的色彩、寬度和樣式。

x1
Single

第一個點的 X 座標。

y1
Single

第一個點的 Y 座標。

x2
Single

第二個點的 X 座標。

y2
Single

第二個點的 Y 座標。

例外狀況

pen null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:

  • 建立黑色畫筆。

  • 建立線條端點的座標。

  • 將線條繪製到畫面。

C#
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);
}

備註

這個方法會繪製一條線,連接 x1y1x2y2 參數指定的兩個點。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET 6, 7, 8, 9
.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, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawLine(Pen, Point, Point)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

繪製連接兩個 Point 結構的線條。

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

參數

pen
Pen

Pen,決定線條的色彩、寬度和樣式。

pt1
Point

Point 結構,表示要連接的第一個點。

pt2
Point

Point 結構,表示要連接的第二個點。

例外狀況

pen null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:

  • 建立黑色畫筆。

  • 建立線條端點的點。

  • 將線條繪製到畫面。

C#
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);
}

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET 6, 7, 8, 9
.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, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawLine(Pen, PointF, PointF)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

繪製連接兩個 PointF 結構的線條。

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

參數

pen
Pen

Pen,決定線條的色彩、寬度和樣式。

pt1
PointF

PointF 結構,表示要連接的第一個點。

pt2
PointF

PointF 結構,表示要連接的第二個點。

例外狀況

pen null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:

  • 建立黑色畫筆。

  • 建立線條端點的點。

  • 將線條繪製到畫面。

C#
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);
}

備註

這個方法會繪製一條線,連接由 pt1 和 p2 參數指定的兩個點。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET 6, 7, 8, 9
.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, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9