指定した位置に指定したサイズで、指定した 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, _ ByVal imageAttr As ImageAttributes, _ ByVal callback As Graphics.DrawImageAbort _)
[C#]
public void DrawImage(Imageimage,RectangledestRect,intsrcX,intsrcY,intsrcWidth,intsrcHeight,GraphicsUnitsrcUnit,ImageAttributesimageAttr,Graphics.DrawImageAbortcallback);
[C++]
public: void DrawImage(Image* image,RectangledestRect,intsrcX,intsrcY,intsrcWidth,intsrcHeight,GraphicsUnitsrcUnit,ImageAttributes* imageAttr,Graphics.DrawImageAbort* callback);
[JScript]
public function DrawImage(
image : Image,destRect : Rectangle,srcX : int,srcY : int,srcWidth : int,srcHeight : int,srcUnit : GraphicsUnit,imageAttr : ImageAttributes,callback : Graphics.DrawImageAbort);
パラメータ
- image
描画する Image オブジェクト。 - destRect
描画イメージの位置とサイズを指定する Rectangle 構造体。イメージは、四角形に合わせてスケーリングされます。 - srcX
描画するソース イメージの一部の左上隅の x 座標。 - srcY
描画するソース イメージの一部の左上隅の y 座標。 - srcWidth
描画するソース イメージの一部の幅。 - srcHeight
描画するソース イメージの一部の高さ。 - srcUnit
抽出元の四角形を決定するために使用する単位を指定する GraphicsUnit 列挙体のメンバ。 - imageAttr
image のカラー変更情報とガンマ情報を指定する ImageAttributes オブジェクト。 - callback
イメージの描画時に呼び出すメソッドを指定する Graphics.DrawImageAbort デリゲート。このメソッドは、アプリケーションにより決定された基準に従って実行された DrawImage メソッドを停止するかどうかをチェックするため頻繁に呼び出されます。
戻り値
このメソッドは値を返しません。
解説
srcX 、 srcY 、 srcWidth 、 srcHeight の各パラメータは、描画する image オブジェクトの四角形部分を指定します。四角形は、ソース イメージの左上隅を起点とします。この部分は destRect オブジェクトで指定された四角形に収まるようにスケーリングされます。
callback パラメータを指定したオーバーローによって、アプリケーションにより決定された基準に従って開始されたイメージの描画を停止するための手段が提供されます。たとえば、アプリケーションが大きなイメージの描画を開始したため、ユーザーがそのイメージをスクロールして画面の外に出すことがあります。この場合、アプリケーションは描画を中断できます。
使用例
[Visual Basic, C#] 次の例は、Windows フォームでの使用を意図してデザインされており、 Paint イベント ハンドラのパラメータである PaintEventArgs e が必要です。このコードは、まず DrawImageAbort デリゲートのコールバック メソッドを定義します。その定義は単純で、 DrawImage メソッドが null 値の callBackData パラメータで呼び出しを行うかどうかをテストするだけです。この例の本文は、次の処理を実行します。
- DrawImageAbort コールバック メソッドのインスタンスを作成します。
- この例が保存されているフォルダの JPEG ファイル SampImag.jpg からイメージを作成します。
- イメージを描画するための四角形を定義する点を作成します。
- 描画するイメージの一部を選択するための四角形を作成します。
- グラフィックス描画単位をピクセルに設定します。
- 画面に元のイメージを描画します。
- 調整済みイメージを描画するための追加の四角形を作成します。
- ガンマ値が通常よりも大きくなるように、調整済みイメージの属性を作成して設定します。
- 画面に調整済みイメージを描画します。
[Visual Basic, C#] 元の未調整の描画先の四角形では、その位置によって画面上のイメージの位置が決まり、抽出元の四角形のサイズおよび描画先の四角形のサイズと形状によって描画イメージのスケーリングが決まります。
[Visual Basic, C#] この例では callBackData パラメータを渡さないオーバーロードを使用するため、 DrawImageAbort コールバックは true を返します。これにより、 DrawImage メソッドは中止され、例に含まれる例外処理コードは、イメージを描画せずに、例外テキストを出力します。
Private Function DrawImageCallback(callBackData As IntPtr) As Boolean
' Test for call that passes callBackData parameter.
If callBackData.Equals(IntPtr.Zero) Then
' If no callBackData passed, abort DrawImage method.
Return True
Else
' If callBackData passed, continue DrawImage method.
Return False
End If
End Function
Public Sub DrawImageRect4IntAtrribAbort(e As PaintEventArgs)
' Create callback method.
Dim imageCallback As New _
Graphics.DrawImageAbort(AddressOf DrawImageCallback)
' Create image.
Dim newImage As Image = Image.FromFile("SampImag.jpg")
' Create rectangle for displaying original image.
Dim destRect1 As New Rectangle(100, 25, 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 original image to screen.
e.Graphics.DrawImage(newImage, destRect1, x, y, width, height, _
units)
' Create rectangle for adjusted image.
Dim destRect2 As New Rectangle(100, 175, 450, 150)
' Create image attributes and set large gamma.
Dim imageAttr As New ImageAttributes()
imageAttr.SetGamma(4F)
Try
' Draw adjusted image to screen.
e.Graphics.DrawImage(newImage, destRect2, x, y, width, _
height, units, imageAttr, imageCallback)
Catch ex As Exception
e.Graphics.DrawString(ex.ToString(), New Font("Arial", 8), _
Brushes.Black, New PointF(0, 0))
End Try
End Sub
[C#]
// Define DrawImageAbort callback method.
private bool DrawImageCallback(IntPtr callBackData)
{
// Test for call that passes callBackData parameter.
if(callBackData==IntPtr.Zero)
{
// If no callBackData passed, abort DrawImage method.
return true;
}
else
{
// If callBackData passed, continue DrawImage method.
return false;
}
}
public void DrawImageRect4IntAtrribAbort(PaintEventArgs e)
{
// Create callback method.
Graphics.DrawImageAbort imageCallback
= new Graphics.DrawImageAbort(DrawImageCallback);
// Create image.
Image newImage = Image.FromFile("SampImag.jpg");
// Create rectangle for displaying original image.
Rectangle destRect1 = new Rectangle(100, 25, 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 original image to screen.
e.Graphics.DrawImage(newImage, destRect1, x, y, width, height, units);
// Create rectangle for adjusted image.
Rectangle destRect2 = new Rectangle(100, 175, 450, 150);
// Create image attributes and set large gamma.
ImageAttributes imageAttr = new ImageAttributes();
imageAttr.SetGamma(4.0F);
try
{
checked
{
// Draw adjusted image to screen.
e.Graphics.DrawImage(
newImage,
destRect2,
x, y,
width, height,
units,
imageAttr,
imageCallback);
}
}
catch (Exception ex)
{
e.Graphics.DrawString(
ex.ToString(),
new Font("Arial", 8),
Brushes.Black,
new PointF(0, 0));
}
}
[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 オーバーロードの一覧