如何:填充开放图形

可以通过将 GraphicsPath 对象传递给 FillPath 方法来填充路径。 FillPath 方法当前为路径设置的填充模式(交替或绕行)填充路径。 如果路径具有任何开放图形,则像这些图形已闭合一样填充路径。 GDI+ 通过绘制一条从终点到起点的直线来闭合图形。

示例

下面的示例创建具有一个开放图形(圆弧)和一个闭合图形(椭圆)的路径。 FillPath 方法根据默认填充模式填充路径,即 Alternate

下图显示了示例代码的输出。 请注意,路径会像开放图形已闭合一样进行填充(根据 Alternate),方法是绘制一条从其终点到其起点的直线。

Diagram that shows the output of the FillPath method

GraphicsPath path = new GraphicsPath();

// Add an open figure.
path.AddArc(0, 0, 150, 120, 30, 120);

// Add an intrinsically closed figure.
path.AddEllipse(50, 50, 50, 100);

Pen pen = new Pen(Color.FromArgb(128, 0, 0, 255), 5);
SolidBrush brush = new SolidBrush(Color.Red);

// The fill mode is FillMode.Alternate by default.
e.Graphics.FillPath(brush, path);
e.Graphics.DrawPath(pen, path);
Dim path As New GraphicsPath()

' Add an open figure.
path.AddArc(0, 0, 150, 120, 30, 120)

' Add an intrinsically closed figure.
path.AddEllipse(50, 50, 50, 100)

Dim pen As New Pen(Color.FromArgb(128, 0, 0, 255), 5)
Dim brush As New SolidBrush(Color.Red)

' The fill mode is FillMode.Alternate by default.
e.Graphics.FillPath(brush, path)
e.Graphics.DrawPath(pen, path)

编译代码

前面的示例专用于 Windows 窗体,它需要 PaintEventArgse,后者是 Paint 事件处理程序的参数。

另请参阅