Graphics オブジェクトの状態の管理

更新 : 2007 年 11 月

Graphics クラスは、GDI+ の中核となる役割を果たします。描画を行う場合は、Graphics オブジェクトを取得し、そのプロパティを設定し、そのメソッド (DrawLineDrawImageDrawString など) を呼び出します。

Graphics オブジェクトの DrawRectangle メソッドを呼び出す例を次に示します。DrawRectangle メソッドに渡される最初の引数は Pen オブジェクトです。

Dim graphics As Graphics = e.Graphics
Dim pen As New Pen(Color.Blue) ' Opaque blue
graphics.DrawRectangle(pen, 10, 10, 200, 100)
Graphics graphics = e.Graphics;
Pen pen = new Pen(Color.Blue);  // Opaque blue
graphics.DrawRectangle(pen, 10, 10, 200, 100);

グラフィックスの状態

Graphics オブジェクトは、DrawLineDrawRectangle などの描画メソッドを用意するだけではありません。Graphics オブジェクトは、次のカテゴリに分類されるグラフィックスの状態も維持します。

  • 画質の設定

  • 変換

  • クリッピング領域

画質の設定

Graphics オブジェクトには、描画対象となる項目の画質に影響する複数のプロパティがあります。たとえば、TextRenderingHint プロパティには、テキストに適用するアンチエイリアシングの種類を指定できます。項目の画質に影響するその他のプロパティには、SmoothingModeCompositingModeCompositingQuality、および InterpolationMode があります。

2 つの楕円を描画する例を次に示します。一方にはスムージング モードとして AntiAlias を設定し、もう一方にはスムージング モードとして HighSpeed を設定しています。

Dim graphics As Graphics = e.Graphics
Dim pen As New Pen(Color.Blue)
      
graphics.SmoothingMode = SmoothingMode.AntiAlias
graphics.DrawEllipse(pen, 0, 0, 200, 100)
graphics.SmoothingMode = SmoothingMode.HighSpeed
graphics.DrawEllipse(pen, 0, 150, 200, 100)
Graphics graphics = e.Graphics;
Pen pen = new Pen(Color.Blue);

graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.DrawEllipse(pen, 0, 0, 200, 100);
graphics.SmoothingMode = SmoothingMode.HighSpeed;
graphics.DrawEllipse(pen, 0, 150, 200, 100);

変換

Graphics オブジェクトは 2 種類の変換 (ワールド変換およびページ変換) を維持しており、これらの変換がその Graphics オブジェクトが描画するすべての項目に適用されます。ワールド変換には、任意のアフィン変換を格納できます。アフィン変換には、スケーリング、回転、反転、傾斜、および平行移動があります。ページ変換は、スケーリングや単位の変更 (ピクセルからインチへの変更など) のために使用できます。詳細については、「座標系と変換」を参照してください。

Graphics オブジェクトのワールド変換およびページ変換を設定する例を次に示します。ワールド変換として、30°の回転を設定します。ページ変換は、2 番目の DrawEllipse に渡される座標がピクセル単位ではなくミリメートル単位で扱われるように設定されます。このコードは、DrawEllipse メソッドをまったく同じように 2 回呼び出します。最初の DrawEllipse 呼び出しにはワールド変換が適用され、2 回目の DrawEllipse 呼び出しには両方の変換 (ワールド変換およびページ変換) が適用されます。

Dim graphics As Graphics = e.Graphics
Dim pen As New Pen(Color.Red)
      
graphics.ResetTransform()
graphics.RotateTransform(30) ' world transformation
graphics.DrawEllipse(pen, 0, 0, 100, 50)
graphics.PageUnit = GraphicsUnit.Millimeter ' page transformation
graphics.DrawEllipse(pen, 0, 0, 100, 50)
Graphics graphics = e.Graphics;
Pen pen = new Pen(Color.Red); 

graphics.ResetTransform();
graphics.RotateTransform(30);                    // world transformation
graphics.DrawEllipse(pen, 0, 0, 100, 50);
graphics.PageUnit = GraphicsUnit.Millimeter;     // page transformation
graphics.DrawEllipse(pen, 0, 0, 100, 50);

結果として得られる 2 つの楕円を次の図に示します。30°の回転は、楕円の中心ではなく、座標系の原点 (クライアント領域の左上隅) を軸として行われます。また、ペン幅の 1 とは、最初の楕円では 1 ピクセルを意味し、2 番目の楕円では 1 ミリメートルを意味します。

楕円

クリッピング領域

Graphics オブジェクトは、その Graphics オブジェクトが描画するすべての項目に適用されるクリッピング領域を維持しています。クリッピング領域を設定するには、SetClip メソッドを呼び出します。

2 つの四角形を交差させることによって十字型の領域を作成する例を次に示します。この領域が、Graphics オブジェクトのクリッピング領域として指定されます。次に、このコードは、描画範囲がクリッピング領域の内側に限定された 2 本の直線を描画します。

Dim graphics As Graphics = e.Graphics
      
' Opaque red, width 5
Dim pen As New Pen(Color.Red, 5)
      
' Opaque aqua
Dim brush As New SolidBrush(Color.FromArgb(255, 180, 255, 255))
      
' Create a plus-shaped region by forming the union of two rectangles.
Dim [region] As New [Region](New Rectangle(50, 0, 50, 150))
[region].Union(New Rectangle(0, 50, 150, 50))
graphics.FillRegion(brush, [region])
      
' Set the clipping region.
graphics.SetClip([region], CombineMode.Replace)
      
' Draw two clipped lines.
graphics.DrawLine(pen, 0, 30, 150, 160)
graphics.DrawLine(pen, 40, 20, 190, 150)
Graphics graphics = e.Graphics;

// Opaque red, width 5
Pen pen = new Pen(Color.Red, 5);  

// Opaque aqua
SolidBrush brush = new SolidBrush(Color.FromArgb(255, 180, 255, 255));  

// Create a plus-shaped region by forming the union of two rectangles.
Region region = new Region(new Rectangle(50, 0, 50, 150));
region.Union(new Rectangle(0, 50, 150, 50));
graphics.FillRegion(brush, region);

// Set the clipping region.
graphics.SetClip(region, CombineMode.Replace);

// Draw two clipped lines.
graphics.DrawLine(pen, 0, 30, 150, 160);
graphics.DrawLine(pen, 40, 20, 190, 150);

クリップされた直線を次の図に示します。

制限されたクリップ領域

参照

概念

入れ子になっているグラフィックス コンテナの使用

その他の技術情報

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