如何填充打开的图形

可以通过将对象传递给GraphicsPathFillPath方法来填充路径。 该方法 FillPath 根据当前为路径设置的填充模式(备用或绕行)填充路径。 如果路径具有任何打开的数字,则路径将填充,就好像这些数字已关闭一样。 GDI+ 通过绘制直线从终点到起始点来关闭该图形。

示例:

以下示例创建了一条路径,该路径包含一个开放的图形(弧线)和一个封闭的图形(椭圆)。 该方法 FillPath 根据默认填充模式(即 Alternate)填充路径。

下图显示了示例代码的输出。 请注意,路径被填充(根据 Alternate)仿佛打开的数字被直线从其终点关闭到其起点。

显示 FillPath 方法输出的图表

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 事件处理程序的参数。

另请参阅