共用方式為


GDI+ 中的開放型和封閉型曲線

下圖顯示兩個曲線:一個是開放型,一個是封閉型。

開放型 & 封閉型曲線

曲線的 Managed 介面

封閉型曲線具有內景,因此可使用筆刷填入色彩。 GDI+ 中的 Graphics 類別提供以下方法,可用來填滿封閉的形狀和曲線:FillRectangleFillEllipseFillPieFillPolygonFillClosedCurveFillPathFillRegion。 當您呼叫其中一種方法時,您必須將其中一個指定的筆刷類型當做引數傳遞 (SolidBrushHatchBrushTextureBrushLinearGradientBrushPathGradientBrush)。

FillPie 方法是 DrawArc 方法的搭配方法。 DrawArc 方法繪製橢圓形外框,而 FillPie 方法則是填滿橢圓形的內景部分。 下列範例會繪製弧形並將橢圓形內景的對應區域填入色彩:

        myGraphics.FillPie(mySolidBrush, 0, 0, 140, 70, 0, 120)
        myGraphics.DrawArc(myPen, 0, 0, 140, 70, 0, 120)

myGraphics.FillPie(mySolidBrush, 0, 0, 140, 70, 0, 120);
myGraphics.DrawArc(myPen, 0, 0, 140, 70, 0, 120);

下圖將顯示弧形和已填滿的扇形圖。

開放型 & 封閉型曲線

FillClosedCurve 方法是 DrawClosedCurve 方法的搭配方法。 上述兩種方法都可透過將結束點連接到起始點來自動封閉曲線。 下列範例會繪製一條通過 (0, 0)、(60, 20) 和 (40, 50) 的曲線。 然後該曲線將透過連接 (40, 50) 和起始點 (0, 0) 自動封閉起來,而且內景將填入純色。

        Dim myPointArray As Point() = _
           {New Point(0, 0), New Point(60, 20), New Point(40, 50)}
        myGraphics.DrawClosedCurve(myPen, myPointArray)
        myGraphics.FillClosedCurve(mySolidBrush, myPointArray)

     Point[] myPointArray =
{ new Point(0, 0), new Point(60, 20), new Point(40, 50) };
     myGraphics.DrawClosedCurve(myPen, myPointArray);
     myGraphics.FillClosedCurve(mySolidBrush, myPointArray);

FillPath 方法可填入路徑個別部分的內景。 如果路徑的某個部分未能形成封閉型曲線或形狀,FillPath 方法會在填入色彩之前,自動將路徑的該部分封閉起來。 下列範例繪製並填滿由弧形、基本曲線、字串和圓形圖所組成的路徑:

        Dim mySolidBrush As New SolidBrush(Color.Aqua)
        Dim myGraphicsPath As New GraphicsPath()

        Dim myPointArray As Point() = { _
           New Point(15, 20), _
           New Point(20, 40), _
           New Point(50, 30)}

        Dim myFontFamily As New FontFamily("Times New Roman")
        Dim myPointF As New PointF(50, 20)
        Dim myStringFormat As New StringFormat()

        myGraphicsPath.AddArc(0, 0, 30, 20, -90, 180)
        myGraphicsPath.AddCurve(myPointArray)
        myGraphicsPath.AddString("a string in a path", myFontFamily, _
           0, 24, myPointF, myStringFormat)
        myGraphicsPath.AddPie(230, 10, 40, 40, 40, 110)

        myGraphics.FillPath(mySolidBrush, myGraphicsPath)
        myGraphics.DrawPath(myPen, myGraphicsPath)

     SolidBrush mySolidBrush = new SolidBrush(Color.Aqua);
     GraphicsPath myGraphicsPath = new GraphicsPath();

     Point[] myPointArray = {
new Point(15, 20), 
new Point(20, 40), 
new Point(50, 30)};

     FontFamily myFontFamily = new FontFamily("Times New Roman");
     PointF myPointF = new PointF(50, 20);
     StringFormat myStringFormat = new StringFormat();

     myGraphicsPath.AddArc(0, 0, 30, 20, -90, 180);
     myGraphicsPath.AddCurve(myPointArray);
     myGraphicsPath.AddString("a string in a path", myFontFamily,
        0, 24, myPointF, myStringFormat);
     myGraphicsPath.AddPie(230, 10, 40, 40, 40, 110);

     myGraphics.FillPath(mySolidBrush, myGraphicsPath);
     myGraphics.DrawPath(myPen, myGraphicsPath);

下圖將顯示純色填滿或未以純色填滿的路徑。 請注意,DrawPath 方法可為字串中的文字繪製出外框,但不會填入色彩。 FillPath 方法將會為字串中的字元內景上色。

路徑中的字串

請參閱

工作

HOW TO:建立繪製的圖形物件

參考

System.Drawing.Drawing2D.GraphicsPath

System.Drawing.Pen

System.Drawing.Point

其他資源

線條、曲線和形狀

建構和繪製路徑