GDI+ 中的开放曲线和闭合曲线

下面的插图显示了两条曲线:一条打开的和一条闭合的。

开放曲线和闭合曲线

曲线的管理界面

闭合的曲线具有内部,因此可以用画笔填充。 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 方法可用于绘制字符串中字符的内部。

路径中的字符串

请参见

任务

如何:创建用于绘制的 Graphics 对象

参考

System.Drawing.Drawing2D.GraphicsPath

System.Drawing.Pen

System.Drawing.Point

其他资源

直线、曲线和图形

构造并绘制轨迹