描画領域をクリップする - リージョンを用いた描画領域のクリップ - C#

C#で描画領域をクリップするコードを紹介します。

概要

Graphicsオブジェクトでびょぐあ領域をクリップする場合は、SetClip()メソッドを利用します。

プログラム

クリップを利用しない描画

まず、クリップを使用しない描画のコードを紹介します。

UI

下図のUIを作成します。フォームを作成するのみです。

コード

下記のコードを記述します。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ClipRegion
{
  public partial class FormMain : Form
  {
    public FormMain()
    {
      InitializeComponent();
    }

    private void FormMain_Paint(object sender, PaintEventArgs e)
    {
      for (int i = -400; i <= 400; i = i + 10) {
        e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Blue)), new Point(0, i), new Point(800, i+800));
      }
    }
  }
}
解説
フォームのPaintメソッドに線を描画するコードを記述します。forループで巡回するため、斜めの線を等間隔で複数本描画する動作になります。

実行結果

プロジェクトを実行します。下図の画面が表示されます。フォーム全体に線が描画されていることが確認できます。

矩形でクリップをする描画

続いて、矩形でクリップできるようコードを追加します。

UI

先のUIと変更はありません

コード

下記のコードを記述します。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ClipRegion
{
  public partial class FormMain : Form
  {
    public FormMain()
    {
      InitializeComponent();
    }

    private void FormMain_Paint(object sender, PaintEventArgs e)
    {
      Rectangle clipRect = new Rectangle(120,120,240,96);
      e.Graphics.SetClip(clipRect);

      for (int i = -400; i <= 400; i = i + 10) {
        e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Blue)), new Point(0, i), new Point(800, i+800));
      }
    }
  }
}
解説
  Rectangle clipRect = new Rectangle(120,120,240,96);
  e.Graphics.SetClip(clipRect);
上記が追加されたコードです。Rectangleオブジェクトを作成し、クリップする領域のRectを作成します。
作成したRectangleオブジェクトをGraphicsオブジェクトのSetClip()メソッドの引数に与えることで、描画領域をクリップします。

実行結果

プロジェクトを実行します。フォームが表示され下図の画面が表示されます。(x=120, y=120, width=240, height=96)の領域のみに斜め線が描画されていることが確認できます。

ポリゴン(任意の多角形)でクリップする描画

ポリゴンでクリップする場合のコードを紹介します。

UI

先のものから変更はありません。

コード

下記のコードを記述します。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ClipRegion
{
  public partial class FormMain : Form
  {
    public FormMain()
    {
      InitializeComponent();
    }

    private void FormMain_Paint(object sender, PaintEventArgs e)
    {
      Point[] points = { new Point(95,95), new Point(360,32), new Point( 250,240)};
      GraphicsPath path = new GraphicsPath();
      path.AddPolygon(points);
      Region region = new Region(path);
      e.Graphics.SetClip(region, CombineMode.Replace);

      for (int i = -400; i <= 400; i = i + 10) {
        e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Blue)), new Point(0, i), new Point(800, i+800));
      }
    }
  }
}
解説
  Point[] points = { new Point(95,95), new Point(360,32), new Point( 250,240)};
  GraphicsPath path = new GraphicsPath();
  path.AddPolygon(points);
  Region region = new Region(path);
  e.Graphics.SetClip(region, CombineMode.Replace);
上記のコードが追加のコードです。
Point[] (Point配列)を作成し、クリップするポリゴンの頂点列を作成します。GraphicsPathオブジェクトを作成し、AddPolygonメソッドを用いてポリゴン頂点列からグラフィックスパスを作成します。
Regionオブジェクトを作成しコンストラクタに、先に作成したグラフィックスパスを与えることで、ポリゴンのリージョンオブジェクトを作成できます。作成し他リージョンをSetClip()メソッドに引数に与えることで描画領域をクリップできます。

実行結果

プロジェクトを実行します。フォームが表示され、下図の画面が表示されます。作成したポリゴン部分で描画がクリップされていることが確認できます。


著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
最終更新日: 2024-01-06
作成日: 2015-02-24
iPentec all rights reserverd.