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)来自动闭合曲线,并用纯色填充内部。

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

FillPath 方法填充路径的单独部分对应的内部区域。 如果路径的某个部分未形成封闭的曲线或形状,FillPath 方法会在填充路径之前自动关闭该部分。 以下示例绘制并填充由弧线、基数自由绘制曲线、字符串和扇形组成的路径:

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

下图显示了具有和不具有实心填充的路径。 请注意,字符串中的文本由 DrawPath 方法概述,但未填充。 绘制字符串中字符内部区域的是 FillPath 方法。

路径中的字符串 中的字符串

另请参阅