GDI+ でのグラフィックス パス

パスは、直線、四角形、およびシンプルな曲線を組み合わせることによって形成されます。 「ベクター グラフィックスの概要」を振り返ると、次の基本的な構成要素は画像を描画するのに最も役立つことが実証されているということでした。

  • 路線

  • 四角形

  • 楕円

  • 円弧

  • 多角形

  • カーディナル スプライン

  • ベジエ スプライン

GDI + では、GraphicsPath オブジェクトを使用すると、これらの構成要素のシーケンスを 1 つの単位にまとめることができます。 そうすると、Graphics クラスの DrawPath メソッドを 1 回呼び出すだけで、直線、四角形、多角形、および曲線のシーケンス全体を描画することができます。 次の図に、直線、円弧、ベジエ スプライン、およびカーディナル スプラインを組み合わせて作成したパスを示します。

Image of a single-line path, starting from a straight line and continuing into different shapes.

パスの使用

GraphicsPath クラスには、描画する項目のシーケンスを作成するために次のメソッドが用意されています: AddLineAddRectangleAddEllipseAddArcAddPolygonAddCurve (カーディナル スプライン用)、および AddBezier。 これらのメソッドはそれぞれオーバーロードされます。つまり、各メソッドで、いくつかの異なるパラメーター リストをサポートしています。 たとえば、4 つの整数を受け取る AddLine メソッドのバリエーションもあれば、2 つの Point オブジェクトを受け取る AddLine メソッドのバリエーションもあります。

直線、長方形、およびベジエ スプラインをパスに追加するためのメソッドには、1 回の呼び出しでパスに複数の項目を追加するための複数のコンパニオン メソッド (AddLinesAddRectangles、および AddBeziers) が含まれています。 また、AddCurve および AddArc メソッドには、閉じた曲線またはパイをパスに追加するためのコンパニオン メソッド AddClosedCurve および AddPie が含まれています。

パスを描画するには、Graphics オブジェクト、Pen オブジェクト、および GraphicsPath オブジェクトが必要です。 Graphics オブジェクトは DrawPath メソッドを備え、Pen オブジェクトにはパスのレンダリングに使用される直線の属性 (幅や色など) が格納されます。 GraphicsPath オブジェクトには、パスを構成する直線と曲線のシーケンスが格納されます。 Pen オブジェクトと GraphicsPath オブジェクトは、DrawPath メソッドに引数として渡されます。 次の例では、直線、楕円、およびベジエ スプラインで構成されるパスを描画します。

myGraphicsPath.AddLine(0, 0, 30, 20);
myGraphicsPath.AddEllipse(20, 20, 20, 40);
myGraphicsPath.AddBezier(30, 60, 70, 60, 50, 30, 100, 10);
myGraphics.DrawPath(myPen, myGraphicsPath);
myGraphicsPath.AddLine(0, 0, 30, 20)
myGraphicsPath.AddEllipse(20, 20, 20, 40)
myGraphicsPath.AddBezier(30, 60, 70, 60, 50, 30, 100, 10)
myGraphics.DrawPath(myPen, myGraphicsPath)

次の図に、そのパスを示します。

Image of a path displayed within a graph.

直線、四角形、および曲線をパスに追加することに加えて、パスにパスを追加することもできます。 これにより、既存のパスを組み合わせて、大規模で複雑なパスを形成することができます。

myGraphicsPath.AddPath(graphicsPath1, false);
myGraphicsPath.AddPath(graphicsPath2, false);
myGraphicsPath.AddPath(graphicsPath1, False)
myGraphicsPath.AddPath(graphicsPath2, False)

他にもパスに追加できる項目が 2 つあります。文字列とパイです。 パイは楕円の内部の一部です。 次の例では、弧、カーディナル スプライン、文字列、およびパイによってパスを作成します。

GraphicsPath myGraphicsPath = new GraphicsPath();

Point[] myPointArray =
{
    new Point(5, 30),
    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.StartFigure();
myGraphicsPath.AddCurve(myPointArray);
myGraphicsPath.AddString("a string in a path", myFontFamily,
   0, 24, myPointF, myStringFormat);
myGraphicsPath.AddPie(230, 10, 40, 40, 40, 110);
myGraphics.DrawPath(myPen, myGraphicsPath);
Dim myGraphicsPath As New GraphicsPath()

Dim myPointArray As Point() = { _
   New Point(5, 30), _
   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.StartFigure()
myGraphicsPath.AddCurve(myPointArray)
myGraphicsPath.AddString("a string in a path", myFontFamily, _
   0, 24, myPointF, myStringFormat)
myGraphicsPath.AddPie(230, 10, 40, 40, 40, 110)
myGraphics.DrawPath(myPen, myGraphicsPath)

次の図に、そのパスを示します。 パスをつなげる必要がないことに注意してください。円弧、カーディナル スプライン、文字列、およびパイは分離されています。

Paths

関連項目