共用方式為


如何:從直線、曲線和形狀建立圖形

若要建立圖形,請建構 GraphicsPath ,然後呼叫 方法,例如 AddLineAddCurve ,以將基本類型新增至路徑。

範例

下列程式碼範例會建立具有數位的路徑:

  • 第一個範例會建立具有單一圖表的路徑。 此圖是由單一弧線所組成。弧線的掃掠角度為 –180 度,在預設座標系統中是逆時針的。

  • 第二個範例會建立具有兩個圖形的路徑。 第一個圖是弧線後面接著一條線。 第二個圖是一條線條,後面接著一條曲線,後面接著一條線。 第一個圖保持開啟狀態,而第二個圖則為關閉狀態。

GraphicsPath path = new GraphicsPath();
path.AddArc(175, 50, 50, 50, 0, -180);
e.Graphics.DrawPath(new Pen(Color.FromArgb(128, 255, 0, 0), 4), path);
Dim path As New GraphicsPath()
path.AddArc(175, 50, 50, 50, 0, -180)
e.Graphics.DrawPath(New Pen(Color.FromArgb(128, 255, 0, 0), 4), path)

     // Create an array of points for the curve in the second figure.
     Point[] points = {
new Point(40, 60),
new Point(50, 70),
new Point(30, 90)};

     GraphicsPath path = new GraphicsPath();

     path.StartFigure(); // Start the first figure.
     path.AddArc(175, 50, 50, 50, 0, -180);
     path.AddLine(100, 0, 250, 20);
     // First figure is not closed.

     path.StartFigure(); // Start the second figure.
     path.AddLine(50, 20, 5, 90);
     path.AddCurve(points, 3);
     path.AddLine(50, 150, 150, 180);
     path.CloseFigure(); // Second figure is closed.

     e.Graphics.DrawPath(new Pen(Color.FromArgb(255, 255, 0, 0), 2), path);
' Create an array of points for the curve in the second figure.
Dim points As Point() = { _
   New Point(40, 60), _
   New Point(50, 70), _
   New Point(30, 90)}

Dim path As New GraphicsPath()

path.StartFigure() ' Start the first figure.
path.AddArc(175, 50, 50, 50, 0, -180)
path.AddLine(100, 0, 250, 20)
' First figure is not closed.

path.StartFigure() ' Start the second figure.
path.AddLine(50, 20, 5, 90)
path.AddCurve(points, 3)
path.AddLine(50, 150, 150, 180)
path.CloseFigure() ' Second figure is closed.
e.Graphics.DrawPath(New Pen(Color.FromArgb(255, 255, 0, 0), 2), path)

編譯程式碼

先前的範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse ,這是事件處理常式的參數 Paint

另請參閱