ホームに戻る
出典 :
目次 :
グラフィックス処理
Graphics オブジェクトを使用することで、グラフィックス処理(図形描画、画像の表示・加工)を行うことができる。
Graphics オブジェクトは描画時のキャンバスとして振舞う。
基本形(線分の描画)

OnPaint() イベントハンドラ上で描画する場合
using System;
using System.Drawing;
using System.Windows.Forms;
class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
class Form1 : Form
{
// フォーム コンストラクタ
public Form1()
{
this.BackColor = SystemColors.Window;
}
// OnPaint() イベントハンドラ
// (フォーム描画時の処理 : オーバーライド)
protected override void OnPaint(PaintEventArgs e)
{
// e からグラフィックスを取得
Graphics g = e.Graphics;
// 青色、太さ 2 のペンを定義
Pen pen = new Pen(Color.Blue, 2);
// (20, 20) から (200, 200) まで直線を描画
g.DrawLine(pen, 20, 20, 200, 200);
// ペンを破棄
pen.Dispose();
}
}
PaintEventArgs は Graphics オブジェクトをメンバとして保持しており、描画に使用することができる。
線を描画する際には Pen (仮想ペン)オブジェクトを使用する。
DrawLine() では始点座標と終点座標を指定する。
任意のタイミングで(描画イベントと無関係に)描画する場合
using System;
using System.Drawing;
using System.Windows.Forms;
:
class Form1 : Form
{
:
void DoSomething()
{
// グラフィックスを作成
Graphics g = CreateGraphics();
// 青色、太さ 2 のペンを定義
Pen pen = new Pen(Color.Blue, 2);
// (20, 20) から (200, 200) まで直線を描画
g.DrawLine(pen, 20, 20, 200, 200);
// ペンを破棄
pen.Dispose();
// グラフィックスを破棄
g.Dispose();
}
}
Form.CreateGraphics() を使用することで、Graphics オブジェクトを都度生成することが可能。
不要となった時点で、Dispose() により破棄する。
矩形、楕円の描画

using System;
using System.Drawing;
using System.Windows.Forms;
:
class Form1 : Form
{
:
void DoSomething()
{
// グラフィックスを作成
Graphics g = CreateGraphics();
// ペンを定義
Pen pen1 = new Pen(Color.Red, 2);
Pen pen2 = new Pen(Color.Blue, 2);
// 図形を描画
g.DrawRectangle(pen1, 20, 20, 200, 200); //< 長方形
g.DrawEllipse(pen2, 20, 20, 200, 200); //< 楕円
// ペンを破棄
pen1.Dispose();
pen2.Dispose();
// グラフィックスを破棄
g.Dispose();
}
}
DrawRectangle() 、DrawEllipse() の引数は、(ペン, 始点x, 始点y, 矩形の幅(x), 矩形の高さ(y))となっている点に注意。
(DrawLine() とは異なる。)
矩形、楕円の塗りつぶし
単色ブラシ(SolidBrush)での塗りつぶし

using System;
using System.Drawing;
using System.Windows.Forms;
:
class Form1 : Form
{
:
void DoSomething()
{
// グラフィックスを作成
Graphics g = CreateGraphics();
// ペンを定義
Pen pen1 = new Pen(Color.Red, 2);
Pen pen2 = new Pen(Color.Blue, 2);
// ブラシを定義
SolidBrush brush = new SolidBrush(Color.Yellow);
// 範囲(矩形)を定義
Rectangle rect = new Rectangle(20, 20, 200, 200);
g.DrawRectangle(pen1, rect); //< 矩形(の輪郭)を描画
g.FillEllipse(brush, rect); //< 楕円を塗り潰し
g.DrawEllipse(pen2, rect); //< 楕円(の輪郭)を描画
// ペンとブラシを破棄
pen1.Dispose();
pen2.Dispose();
brush.Dispose();
// グラフィックスを破棄
g.Dispose();
}
}
単色での塗りつぶしには SolidBrush を用いる。
「塗りつぶし」⇒「輪郭描画」の順に実行しないと、輪郭が塗りつぶしによって隠れてしまう点に注意。
ハッチブラシ(HatchBrush)、線形グラデーションブラシ(LinearGradientBrush)での塗りつぶし

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
:
class Form1 : Form
{
:
void DoSomething()
{
// グラフィックスを作成
Graphics g = CreateGraphics();
// 範囲(矩形)を定義
Rectangle rect = new Rectangle(20, 20, 200, 200);
// ペンを定義
Pen pen1 = new Pen(Color.Blue, 1);
Pen pen2 = new Pen(Color.Orange, 1);
// ハッチブラシを定義
HatchBrush brush1 = new HatchBrush(HatchStyle.Cross, //< スタイル
Color.Blue, //< 前景色
Color.Azure); //< 背景色
// 線型グラデーションブラシを定義
LinearGradientBrush brush2 = new LinearGradientBrush(rect, //< 色変化範囲
Color.Yellow, //< 始点色
Color.White, //< 終点色
LinearGradientMode.ForwardDiagonal); //< グラデーションの方向
e.Graphics.FillRectangle(brush1, rect); //< 矩形を塗りつぶし
e.Graphics.DrawRectangle(pen1, rect); //< 矩形(の輪郭)を描画
e.Graphics.FillEllipse(brush2, rect); //< 楕円を塗りつぶし
e.Graphics.DrawEllipse(pen2, rect); //< 楕円(の輪郭)を描画
// ペンとブラシを破棄
pen1.Dispose();
pen2.Dispose();
brush1.Dispose();
brush2.Dispose();
// グラフィックスを破棄
g.Dispose();
}
}
HatchBrush 、LinearGradientBrush は System.Drawing.Drawing2D 名前空間に属する。
描画・塗りつぶしメソッド(Graphics)
描画メソッド | 塗りつぶしメソッド | 対象 | FillModeの使用 |
DrawLine | | 直線 | |
DrawClosedCurve | FillClosedCurve | 閉じたカーディナル スプライン | ○ |
DrawEllipse | FillEllipse | 楕円 | |
DrawPath | FillPath | GraphicsPath | (GraphicsPathで使用) |
DrawArc | | 円弧 | |
DrawPie | FillPie | 扇形 | |
DrawPolygon | FillPolygon | 多角形 | ○ |
DrawRectangle | FillRectangle | 四角形 | |
DrawRectangles | FillRectangles | 一連の四角形 | |
| FillRegion | Region |
|
その他のグラフィックス
出典元を参照。