次の方法で共有


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

パスは、線、四角形、単純曲線を組み合わせて形成されます。 ベクター グラフィックスの概要 から、次の基本的な構成要素が図の描画に最も役立つことが証明されていることを思い出してください。

  • 四角 形

  • 楕円

  • 円弧

  • ポリゴン

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

  • ベジエ スプライン

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

直線から始まり、さまざまな図形に続く単一行パスの画像。

パスの使用

GraphicsPath クラスは、描画する項目のシーケンスを作成するためのメソッドを提供します。AddLineAddRectangleAddEllipseAddArcAddPolygonAddCurve (カーディナル スプラインの場合)、および AddBezier。 これらの各メソッドはオーバーロードされます。つまり、各メソッドは複数の異なるパラメーター リストをサポートします。 たとえば、AddLine メソッドの 1 つのバリエーションは 4 つの整数を受け取り、別のバリエーションの AddLine メソッドは 2 つの Point オブジェクトを受け取ります。

パスに線、四角形、ベジエ スプラインを追加するメソッドには、複数の項目を 1 回の呼び出しでパスに追加する複数のコンパニオン メソッドがあります。AddLinesAddRectanglesAddBeziers。 また、AddCurve メソッドと AddArc メソッドには、パスに閉じた曲線または円を追加するコンパニオン メソッド (AddClosedCurveAddPie) があります。

パスを描画するには、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)

次の図は、パスを示しています。

グラフ内に表示されるパスの画像。

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

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)

次の図は、パスを示しています。 パスを接続する必要はありません。円弧、カーディナル スプライン、文字列、および円が分離されます。

パス

関連項目