次の方法で共有


方法 : ペンの幅と配置を設定する

更新 : 2007 年 11 月

Pen オブジェクトを作成するときに、そのコンストラクタへの引数の 1 つとしてペン幅を渡すことができます。また、Pen クラスの Width プロパティを使用して、ペン幅を変更することもできます。

理論上は、線の幅は 0 です。幅が 1 ピクセルの線を描画する場合、そのピクセルは理論上の線の中央に配置されます。幅が 2 ピクセル以上の直線を描画する場合は、ピクセルは理論上の線の中央か、その線の片側に揃えて配置されます。Pen のペン配置プロパティを設定して、そのペンで描画されるピクセルを理論上の線に対してどのように配置するかを指定できます。

次のコード例の CenterOutset、および Inset の各値は、PenAlignment 列挙体のメンバです。

1 本の直線を 2 回描画する例を次に示します。1 回目は幅が 1 の黒いペンで描画し、2 回目は幅が 10 の緑のペンで描画します。

ペンの幅を変更するには

  • Alignment プロパティの値を Center (既定値) に設定して、緑のペンで描画されるピクセルが理論上の直線の中央に揃えて配置されるように指定します。結果として得られる直線を次の図に示します。

    ペン

    四角形を 2 回描画する例を次に示します。1 回目は幅が 1 の黒いペンで描画し、2 回目は幅が 10 の緑のペンで描画します。

    Dim blackPen As New Pen(Color.FromArgb(255, 0, 0, 0), 1)
    Dim greenPen As New Pen(Color.FromArgb(255, 0, 255, 0), 10)
    greenPen.Alignment = PenAlignment.Center
    
    ' Draw the line with the wide green pen.
    e.Graphics.DrawLine(greenPen, 10, 100, 100, 50)
    
    ' Draw the line with the thin black pen.
    e.Graphics.DrawLine(blackPen, 10, 100, 100, 50)
    
    
    Pen blackPen = new Pen(Color.FromArgb(255, 0, 0, 0), 1);
    Pen greenPen = new Pen(Color.FromArgb(255, 0, 255, 0), 10);
    greenPen.Alignment = PenAlignment.Center;
    
    // Draw the line with the wide green pen.
    e.Graphics.DrawLine(greenPen, 10, 100, 100, 50);
    
    // Draw the line with the thin black pen.
    e.Graphics.DrawLine(blackPen, 10, 100, 100, 50);
    

ペンの配置を変更するには

  • Alignment プロパティの値を Center に設定して、緑のペンで描画されるピクセルが四角形の境界の中央に揃えて配置されるように指定します。

    結果として得られる四角形を次の図に示します。

    ペン

    Dim blackPen As New Pen(Color.FromArgb(255, 0, 0, 0), 1)
    Dim greenPen As New Pen(Color.FromArgb(255, 0, 255, 0), 10)
    greenPen.Alignment = PenAlignment.Center
    
    ' Draw the rectangle with the wide green pen.
    e.Graphics.DrawRectangle(greenPen, 10, 100, 50, 50)
    
    ' Draw the rectangle with the thin black pen.
    e.Graphics.DrawRectangle(blackPen, 10, 100, 50, 50)
    
    
    Pen blackPen = new Pen(Color.FromArgb(255, 0, 0, 0), 1);
    Pen greenPen = new Pen(Color.FromArgb(255, 0, 255, 0), 10);
    greenPen.Alignment = PenAlignment.Center;
    
    // Draw the rectangle with the wide green pen.
    e.Graphics.DrawRectangle(greenPen, 10, 100, 50, 50);
    
    // Draw the rectangle with the thin black pen.
    e.Graphics.DrawRectangle(blackPen, 10, 100, 50, 50);
    

くぼみ表示のペンを作成するには

  • 上のコードの 3 番目のステートメントを次のように変更して、緑のペンの配置を変更します。

    greenPen.Alignment = PenAlignment.Inset
    
    
    greenPen.Alignment = PenAlignment.Inset;
    

    この場合、緑の太い直線を表すピクセルは、次の図で示すように四角形の内側に表示されます。

    ペン

参照

その他の技術情報

ペンを使用した直線と図形の描画

Windows フォームにおけるグラフィックスと描画