次の方法で共有


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

更新 : 2007 年 11 月

パスは、直線、四角形、および単純な曲線を組み合わせて作成されます。ピクチャの描画に最も適した基本的なビルド ブロックとして次の図形を使用できることは、「ベクタ グラフィックスの概要」で既に説明しました。

  • 直線

  • 四角形

  • 楕円

  • 円弧

  • 多角形

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

  • ベジエ スプライン

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

パス

パスの使用

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

直線、四角形、およびベジエ スプラインをパスに追加するメソッドには、1 回の呼び出しで複数の項目をパスに追加するコンパニオン メソッドである AddLinesAddRectangles、および AddBeziers があります。また、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 種類の項目をパスに追加できます。扇形は、楕円の内部にある一部分です。円弧、カーディナル スプライン、文字列、および扇形からパスを作成する例を次に示します。

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)

     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);

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

パス

参照

処理手順

方法 : 描画する Graphics オブジェクトを作成する

参照

System.Drawing.Drawing2D.GraphicsPath

System.Drawing.Point

その他の技術情報

直線、曲線、および図形

パスの作成および描画