図を作成するには、 GraphicsPathを構築し、 AddLine や AddCurveなどのメソッドを呼び出して、パスにプリミティブを追加します。
例
次のコード例では、図を含むパスを作成します。
最初の例では、1 つの図形を含むパスを作成します。 図は 1 つの円弧で構成されています。円弧のスイープ角度は –180 度で、既定の座標系では反時計回りに設定されます。
2 番目の例では、2 つの図形を含むパスを作成します。 最初の図は、円弧の後に線が続きます。 2 番目の図は、線があり、それに続いて曲線があり、さらに線が続きます。 最初の図は開いたままで、2 番目の図は閉じています。
GraphicsPath path = new GraphicsPath();
path.AddArc(175, 50, 50, 50, 0, -180);
e.Graphics.DrawPath(new Pen(Color.FromArgb(128, 255, 0, 0), 4), path);
Dim path As New GraphicsPath()
path.AddArc(175, 50, 50, 50, 0, -180)
e.Graphics.DrawPath(New Pen(Color.FromArgb(128, 255, 0, 0), 4), path)
// Create an array of points for the curve in the second figure.
Point[] points = {
new Point(40, 60),
new Point(50, 70),
new Point(30, 90)};
GraphicsPath path = new GraphicsPath();
path.StartFigure(); // Start the first figure.
path.AddArc(175, 50, 50, 50, 0, -180);
path.AddLine(100, 0, 250, 20);
// First figure is not closed.
path.StartFigure(); // Start the second figure.
path.AddLine(50, 20, 5, 90);
path.AddCurve(points, 3);
path.AddLine(50, 150, 150, 180);
path.CloseFigure(); // Second figure is closed.
e.Graphics.DrawPath(new Pen(Color.FromArgb(255, 255, 0, 0), 2), path);
' Create an array of points for the curve in the second figure.
Dim points As Point() = { _
New Point(40, 60), _
New Point(50, 70), _
New Point(30, 90)}
Dim path As New GraphicsPath()
path.StartFigure() ' Start the first figure.
path.AddArc(175, 50, 50, 50, 0, -180)
path.AddLine(100, 0, 250, 20)
' First figure is not closed.
path.StartFigure() ' Start the second figure.
path.AddLine(50, 20, 5, 90)
path.AddCurve(points, 3)
path.AddLine(50, 150, 150, 180)
path.CloseFigure() ' Second figure is closed.
e.Graphics.DrawPath(New Pen(Color.FromArgb(255, 255, 0, 0), 2), path)
コードのコンパイル
前の例は Windows フォームで使用できるように設計されており、PaintEventArgs イベント ハンドラーのパラメーターであるe
Paintが必要です。
こちらも参照ください
- GraphicsPath
- パスの構築と描画
- ペンを使用して線や図形を描画する
GitHub で Microsoft と共同作業する
このコンテンツのソースは GitHub にあります。そこで、issue や pull request を作成および確認することもできます。 詳細については、共同作成者ガイドを参照してください。
.NET Desktop feedback