次の方法で共有


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

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

Overloads Public Sub DrawImage( _
   ByVal image As Image, _   ByVal destRect As RectangleF, _   ByVal srcRect As RectangleF, _   ByVal srcUnit As GraphicsUnit _)
[C#]
public void DrawImage(Imageimage,RectangleFdestRect,RectangleFsrcRect,GraphicsUnitsrcUnit);
[C++]
public: void DrawImage(Image* image,RectangleFdestRect,RectangleFsrcRect,GraphicsUnitsrcUnit);
[JScript]
public function DrawImage(
   image : Image,destRect : RectangleF,srcRect : RectangleF,srcUnit : GraphicsUnit);

パラメータ

  • image
    描画する Image オブジェクト。
  • destRect
    描画イメージの位置とサイズを指定する Rectangle 構造体。イメージは、四角形に合わせてスケーリングされます。
  • srcRect
    描画する image オブジェクトの部分を指定する Rectangle 構造体。
  • srcUnit
    srcRect パラメータで使用する単位を指定する GraphicsUnit 列挙体のメンバ。

戻り値

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

解説

描画する image オブジェクトの四角形部分を指定する srcRect パラメータ。この部分は、 destRect パラメータで指定された四角形の内側に収まるようにスケーリングされます。

使用例

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

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

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

 
Public Sub DrawImageRectFRectF(e As PaintEventArgs)
' Create image.
Dim newImage As Image = Image.FromFile("SampImag.jpg")
' Create rectangle for displaying image.
Dim destRect As New RectangleF(100F, 100F, 450F, 150F)
' Create rectangle for source image.
Dim srcRect As New RectangleF(50F, 50F, 150F, 150F)
Dim units As GraphicsUnit = GraphicsUnit.Pixel
' Draw image to screen.
e.Graphics.DrawImage(newImage, destRect, srcRect, units)
End Sub
        
[C#] 
public void DrawImageRectFRectF(PaintEventArgs e)
{
// Create image.
Image newImage = Image.FromFile("SampImag.jpg");
// Create rectangle for displaying image.
RectangleF destRect = new RectangleF( 100.0F, 100.0F, 450.0F, 150.0F);
// Create rectangle for source image.
RectangleF srcRect = new RectangleF( 50.0F, 50.0F, 150.0F, 150.0F);
GraphicsUnit units = GraphicsUnit.Pixel;
// Draw image to screen.
e.Graphics.DrawImage(newImage, destRect, srcRect, 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 オーバーロードの一覧