方法: 開いている図形を塗りつぶす

パスを塗りつぶすには、FillPath メソッドに GraphicsPath オブジェクトを渡します。 FillPath メソッドを使用すると、パスに現在設定されている塗りつぶしモード (交互または巻線) に従ってパスが塗りつぶされます。 パスに開いている図形がある場合は、それらの図形が閉じているかのようにパスが塗りつぶされます。 GDI+ により、図形の終点から始点まで直線が描画され、図形が閉じられます。

次の例では、開いた図形 (円弧) と閉じた図形 (楕円) が 1 つずつあるパスを作成します。 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 フォームで使用するために設計されていて、PaintEventArgs イベント ハンドラーのパラメーターである ePaint を必要とします。

関連項目