GDI+ を使用して線や図形を描画したり、テキストをレンダリングしたり、イメージを表示および操作したりする前に、 Graphics オブジェクトを作成する必要があります。 Graphics オブジェクトは GDI+ 描画サーフェイスを表し、グラフィカル イメージの作成に使用されるオブジェクトです。
グラフィックスの操作には、次の 2 つの手順があります。
グラフィックス オブジェクトの作成
グラフィックス オブジェクトは、さまざまな方法で作成できます。
グラフィックス オブジェクトを作成するには
フォームまたはコントロールのPaintEventArgs イベントのPaintの一部として、グラフィックス オブジェクトへの参照を受け取ります。 これは通常、コントロールの描画コードを作成するときにグラフィックス オブジェクトへの参照を取得する方法です。 同様に、PrintPageEventArgsのPrintPage イベントを処理するときに、グラフィックス オブジェクトをPrintDocumentのプロパティとして取得することもできます。
-又は-
コントロールまたはフォームの CreateGraphics メソッドを呼び出して、そのコントロールまたはフォームの描画サーフェイスを表す Graphics オブジェクトへの参照を取得します。 既に存在するフォームまたはコントロールに描画する場合は、このメソッドを使用します。
-又は-
Graphicsから継承する任意のオブジェクトからImage オブジェクトを作成します。 この方法は、既存のイメージを変更する場合に便利です。
以下のセクションでは、これらの各プロセスについて詳しく説明します。
Paint イベント ハンドラーの PaintEventArgs
コントロールのPaintEventHandlerまたはPrintPageのPrintDocumentをプログラミングする場合、グラフィックス オブジェクトは、PaintEventArgsまたはPrintPageEventArgsのプロパティの 1 つとして提供されます。
Paint イベントの PaintEventArgs から Graphics オブジェクトへの参照を取得するには
Graphics オブジェクトを宣言します。
変数を割り当てて、Graphicsの一部として渡されたPaintEventArgs オブジェクトを参照します。
フォームまたはコントロールを描画するコードを挿入します。
次の例は、Graphics イベントのPaintEventArgsからPaint オブジェクトを参照する方法を示しています。
Private Sub Form1_Paint(sender As Object, pe As PaintEventArgs) Handles _ MyBase.Paint ' Declares the Graphics object and sets it to the Graphics object ' supplied in the PaintEventArgs. Dim g As Graphics = pe.Graphics ' Insert code to paint the form here. End Sub
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs pe) { // Declares the Graphics object and sets it to the Graphics object // supplied in the PaintEventArgs. Graphics g = pe.Graphics; // Insert code to paint the form here. }
private: void Form1_Paint(System::Object ^ sender, System::Windows::Forms::PaintEventArgs ^ pe) { // Declares the Graphics object and sets it to the Graphics object // supplied in the PaintEventArgs. Graphics ^ g = pe->Graphics; // Insert code to paint the form here. }
CreateGraphics メソッド
コントロールまたはフォームの CreateGraphics メソッドを使用して、そのコントロールまたはフォームの描画サーフェイスを表す Graphics オブジェクトへの参照を取得することもできます。
CreateGraphics メソッドを使用して Graphics オブジェクトを作成するには
グラフィックスをレンダリングするフォームまたはコントロールの CreateGraphics メソッドを呼び出します。
Dim g as Graphics ' Sets g to a Graphics object representing the drawing surface of the ' control or form g is a member of. g = Me.CreateGraphics
Graphics g; // Sets g to a graphics object representing the drawing surface of the // control or form g is a member of. g = this.CreateGraphics();
Graphics ^ g; // Sets g to a graphics object representing the drawing surface of the // control or form g is a member of. g = this->CreateGraphics();
Image オブジェクトから作成する
さらに、 Image クラスから派生する任意のオブジェクトからグラフィックス オブジェクトを作成できます。
イメージから Graphics オブジェクトを作成するには
Graphics.FromImage メソッドを呼び出し、Graphics オブジェクトを作成する Image 変数の名前を指定します。
次の例は、 Bitmap オブジェクトを使用する方法を示しています。
Dim myBitmap as New Bitmap("C:\Documents and Settings\Joe\Pics\myPic.bmp") Dim g as Graphics = Graphics.FromImage(myBitmap)
Bitmap myBitmap = new Bitmap(@"C:\Documents and Settings\Joe\Pics\myPic.bmp"); Graphics g = Graphics.FromImage(myBitmap);
Bitmap ^ myBitmap = gcnew Bitmap("D:\\Documents and Settings\\Joe\\Pics\\myPic.bmp"); Graphics ^ g = Graphics::FromImage(myBitmap);
注
Graphics オブジェクトは、16 ビット、24 ビット、32 ビットの .bmp ファイルなど、インデックスのない .bmp ファイルからのみ作成できます。 インデックスのない .bmp ファイルの各ピクセルは、インデックス付き .bmp ファイルのピクセルとは対照的に、色を保持します。これは、カラー テーブルへのインデックスを保持します。
図形と画像の描画と操作
作成後、 Graphics オブジェクトを使用して、線や図形の描画、テキストのレンダリング、画像の表示と操作を行います。 Graphics オブジェクトで使用されるプリンシパル オブジェクトは次のとおりです。
Pen クラス - 線の描画、図形のアウトライン表示、その他の幾何学的表現のレンダリングに使用します。
Brush クラス - 塗りつぶされた図形、画像、テキストなどのグラフィックス領域を塗りつぶす場合に使用します。
Font クラス - テキストをレンダリングするときに使用する図形の説明を提供します。
Color構造 - 表示するさまざまな色を表します。
作成した Graphics オブジェクトを使用するには
上記の適切なオブジェクトを使用して、必要なものを描画します。
詳細については、次のトピックを参照してください。
レンダリングするには 参照先 線 方法: Windows フォーム に線を描画する 図形 図形の輪郭を描く方法 テキスト 方法: Windows フォーム にテキストを描画する 画像 方法: GDI+ を使用してイメージをレンダリングする
こちらも参照ください
.NET Desktop feedback