共用方式為


Open and Closed Curves in GDI+

The following illustration shows two curves: one open and one closed.

Open & Closed curves

Managed Interface for Curves

Closed curves have an interior and therefore can be filled with a brush. The Graphics class in GDI+ provides the following methods for filling closed shapes and curves: FillRectangle, FillEllipse, FillPie, FillPolygon, FillClosedCurve, FillPath, and FillRegion. Whenever you call one of these methods, you must pass one of the specific brush types (SolidBrush, HatchBrush, TextureBrush, LinearGradientBrush, or PathGradientBrush) as an argument.

The FillPie method is a companion to the DrawArc method. Just as the DrawArc method draws a portion of the outline of an ellipse, the FillPie method fills a portion of the interior of an ellipse. The following example draws an arc and fills the corresponding portion of the interior of the ellipse:

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);

The following illustration shows the arc and the filled pie.

Open & Closed curves

The FillClosedCurve method is a companion to the DrawClosedCurve method. Both methods automatically close the curve by connecting the ending point to the starting point. The following example draws a curve that passes through (0, 0), (60, 20), and (40, 50). Then, the curve is automatically closed by connecting (40, 50) to the starting point (0, 0), and the interior is filled with a solid color.

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);

The FillPath method fills the interiors of the separate pieces of a path. If a piece of a path doesn't form a closed curve or shape, the FillPath method automatically closes that piece of the path before filling it. The following example draws and fills a path that consists of an arc, a cardinal spline, a string, and a pie:

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);

The following illustration shows the path with and without the solid fill. Note that the text in the string is outlined, but not filled, by the DrawPath method. It is the FillPath method that paints the interiors of the characters in the string.

String in a path

See Also

Tasks

How to: Create Graphics Objects for Drawing

Reference

System.Drawing.Drawing2D.GraphicsPath
System.Drawing.Pen
System.Drawing.Point

Other Resources

Lines, Curves, and Shapes
Constructing and Drawing Paths