次の方法で共有


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

指定した位置にイメージの一部を描画します。

Overloads Public Sub DrawImage( _
   ByVal image As Image, _   ByVal x As Integer, _   ByVal y As Integer, _   ByVal srcRect As Rectangle, _   ByVal srcUnit As GraphicsUnit _)
[C#]
public void DrawImage(Imageimage,intx,inty,RectanglesrcRect,GraphicsUnitsrcUnit);
[C++]
public: void DrawImage(Image* image,intx,inty,RectanglesrcRect,GraphicsUnitsrcUnit);
[JScript]
public function DrawImage(
   image : Image,x : int,y : int,srcRect : Rectangle,srcUnit : GraphicsUnit);

パラメータ

  • image
    描画する Image オブジェクト。
  • x
    描画イメージの左上隅の x 座標。
  • y
    描画イメージの左上隅の y 座標。
  • srcRect
    描画する image オブジェクトの部分を指定する Rectangle 構造体。
  • srcUnit
    srcRect パラメータで使用する単位を指定する GraphicsUnit 列挙体のメンバ。

戻り値

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

解説

Image オブジェクトは、ピクセル幅の値および水平方向の解像度 (dpi) の値を格納します。イメージの物理的な幅 (インチ) は、ピクセル幅を水平解像度で割った値です。たとえば、幅が 360 ピクセルで、水平解像度が 72 dpi のイメージの物理的な幅は 5 インチになります。ピクセルの高さと物理的な高さについても同様です。

このメソッドは物理サイズを使用してイメージの一部を描画するため、イメージのその部分は表示デバイスの解像度 (dpi) に関係なく、インチで指定された正しいサイズで描画されます。たとえば、幅が 216 ピクセルで、水平解像度が 72 dpi であるイメージの一部を想定します。このメソッドを呼び出して、96 dpi (1 インチあたりのドット数) の解像度のデバイスにそのイメージの部分を描画すると、レンダリングされたイメージの幅は (216/72)*96 = 288 (ピクセル) になります。

使用例

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

[Visual Basic, C#] この例が保存されているフォルダの JPEG ファイル SampImag.jpg からイメージを作成します。

  • イメージの左上隅を描画する座標を作成します。
  • イメージの一部を抽出するための四角形を作成します。
  • 抽出元の四角形の単位をピクセルに設定します。
  • 画面にイメージを描画します。

[Visual Basic, C#] 抽出元の四角形のサイズによって、スケーリングされない元のイメージのどの部分が画面に描画されるかが決まります。

 
Public Sub DrawImage2IntRect(e As PaintEventArgs)
' Create image.
Dim newImage As Image = Image.FromFile("SampImag.jpg")
' Create coordinates for upper-left corner of image.
Dim x As Integer = 100
Dim y As Integer = 100
' Create rectangle for source image.
Dim srcRect As New Rectangle(50, 50, 150, 150)
Dim units As GraphicsUnit = GraphicsUnit.Pixel
' Draw image to screen.
e.Graphics.DrawImage(newImage, x, y, srcRect, units)
End Sub
        
[C#] 
public void DrawImage2IntRect(PaintEventArgs e)
{
// Create image.
Image newImage = Image.FromFile("SampImag.jpg");
// Create coordinates for upper-left corner of image.
int x = 100;
int y = 100;
// Create rectangle for source image.
Rectangle srcRect = new Rectangle( 50, 50, 150, 150);
GraphicsUnit units = GraphicsUnit.Pixel;
// Draw image to screen.
e.Graphics.DrawImage(newImage, x, y, 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 ファミリ, .NET Compact Framework - Windows CE .NET

参照

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