次の方法で共有


Graphics.DrawImage メソッド (Image, Rectangle, Int32, Int32, Int32, Int32, GraphicsUnit)

指定した位置に指定したサイズで、指定した Image オブジェクトの指定した部分を描画します。

Overloads Public Sub DrawImage( _
   ByVal image As Image, _   ByVal destRect As Rectangle, _   ByVal srcX As Integer, _   ByVal srcY As Integer, _   ByVal srcWidth As Integer, _   ByVal srcHeight As Integer, _   ByVal srcUnit As GraphicsUnit _)
[C#]
public void DrawImage(Imageimage,RectangledestRect,intsrcX,intsrcY,intsrcWidth,intsrcHeight,GraphicsUnitsrcUnit);
[C++]
public: void DrawImage(Image* image,RectangledestRect,intsrcX,intsrcY,intsrcWidth,intsrcHeight,GraphicsUnitsrcUnit);
[JScript]
public function DrawImage(
   image : Image,destRect : Rectangle,srcX : int,srcY : int,srcWidth : int,srcHeight : int,srcUnit : GraphicsUnit);

パラメータ

  • image
    描画する Image オブジェクト。
  • destRect
    描画イメージの位置とサイズを指定する Rectangle 構造体。イメージは、四角形に合わせてスケーリングされます。
  • srcX
    描画するソース イメージの一部の左上隅の x 座標。
  • srcY
    描画するソース イメージの一部の左上隅の y 座標。
  • srcWidth
    描画するソース イメージの一部の幅。
  • srcHeight
    描画するソース イメージの一部の高さ。
  • srcUnit
    抽出元の四角形を決定するために使用する単位を指定する GraphicsUnit 列挙体のメンバ。

戻り値

このメソッドは値を返しません。

解説

srcXsrcYsrcWidthsrcHeight の各パラメータは、描画する image オブジェクトの四角形部分を指定します。四角形は、ソース イメージの左上隅を起点とします。この部分は、 destRect パラメータで指定された四角形の内側に収まるようにスケーリングされます。

使用例

[Visual Basic, C#] 次の例は、Windows フォームでの使用を意図してデザインされており、 Paint イベント ハンドラのパラメータである PaintEventArgs e が必要です。このコードは次のアクションを実行します。

  • この例が保存されているフォルダの JPEG ファイル SampImag.jpg からイメージを作成します。
  • イメージを描画するための四角形を作成します。
  • イメージの一部を抽出するための四角形の座標を作成します。
  • 抽出元の四角形の単位をピクセルに設定します。
  • 画面にイメージを描画します。

[Visual Basic, C#] 描画先の四角形の位置によって画面上のイメージの位置が決まり、抽出元および描画先の四角形のサイズによって描画イメージのスケーリングが決まり、抽出元の四角形のサイズによって画面に描画する元のイメージの部分が決まります。

 
Public Sub DrawImageRect4Int(e As PaintEventArgs)
' Create image.
Dim newImage As Image = Image.FromFile("SampImag.jpg")
' Create rectangle for displaying image.
Dim destRect As New Rectangle(100, 100, 450, 150)
' Create coordinates of rectangle for source image.
Dim x As Integer = 50
Dim y As Integer = 50
Dim width As Integer = 150
Dim height As Integer = 150
Dim units As GraphicsUnit = GraphicsUnit.Pixel
' Draw image to screen.
e.Graphics.DrawImage(newImage, destRect, x, y, width, height, _
units)
End Sub
        
[C#] 
public void DrawImageRect4Int(PaintEventArgs e)
{
// Create image.
Image newImage = Image.FromFile("SampImag.jpg");
// Create rectangle for displaying image.
Rectangle destRect = new Rectangle( 100, 100, 450, 150);
// Create coordinates of rectangle for source image.
int x = 50;
int y = 50;
int width = 150;
int height = 150;
GraphicsUnit units = GraphicsUnit.Pixel;
// Draw image to screen.
e.Graphics.DrawImage(newImage, destRect, x, y, width, height, units);
}
        

[C++, JScript] C++ および JScript のサンプルはありません。Visual Basic および C# のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

Graphics クラス | Graphics メンバ | System.Drawing 名前空間 | Graphics.DrawImage オーバーロードの一覧