次の方法で共有


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

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

Overloads Public Sub DrawImage( _
   ByVal image As Image, _   ByVal destPoints() As Point, _   ByVal srcRect As Rectangle, _   ByVal srcUnit As GraphicsUnit, _   ByVal imageAttr As ImageAttributes, _   ByVal callback As Graphics.DrawImageAbort, _   ByVal callbackData As Integer _)
[C#]
public void DrawImage(Imageimage,Point[] destPoints,RectanglesrcRect,GraphicsUnitsrcUnit,ImageAttributesimageAttr,Graphics.DrawImageAbortcallback,intcallbackData);
[C++]
public: void DrawImage(Image* image,PointdestPoints[],RectanglesrcRect,GraphicsUnitsrcUnit,ImageAttributes* imageAttr,Graphics.DrawImageAbort* callback,intcallbackData);
[JScript]
public function DrawImage(
   image : Image,destPoints : Point[],srcRect : Rectangle,srcUnit : GraphicsUnit,imageAttr : ImageAttributes,callback : Graphics.DrawImageAbort,callbackData : int);

パラメータ

  • image
    描画する Image オブジェクト。
  • destPoints
    平行四辺形を定義する 3 つの Point 構造体の配列。
  • srcRect
    描画する image オブジェクトの部分を指定する Rectangle 構造体。
  • srcUnit
    srcRect パラメータで使用する単位を指定する GraphicsUnit 列挙体のメンバ。
  • imageAttr
    image オブジェクトのカラー変更情報とガンマ情報を指定する ImageAttributes オブジェクト。
  • callback
    イメージの描画時に呼び出すメソッドを指定する Graphics.DrawImageAbort デリゲート。このメソッドは、アプリケーションにより決定された基準に従って実行された DrawImage メソッドを停止するかどうかをチェックするため頻繁に呼び出されます。
  • callbackData
    DrawImage メソッドの実行を停止するかどうかをチェックするときに使用する、 DrawImageAbort デリゲートの追加データを指定する値。

戻り値

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

解説

destPoints パラメータは、平行四辺形の 3 つの点を指定します。3 つの Point 構造体は、平行四辺形の左上隅、右上隅、および左下隅を表します。初めの 3 つの点から 4 番目の点を推定して、平行四辺形を形成します。

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

callback パラメータおよび callbackData パラメータを指定したオーバーロードによって、アプリケーションにより決定された基準とデータに従って開始されたイメージの描画を停止する手段が提供されます。たとえば、アプリケーションが大きなイメージの描画を開始したため、ユーザーがそのイメージをスクロールして画面の外に出すことがあります。この場合、アプリケーションは描画を中断できます。

使用例

[Visual Basic, C#] 次の例は、Windows フォームでの使用を意図してデザインされており、 Paint イベント ハンドラのパラメータである PaintEventArgs e が必要です。このコードは、まず DrawImageAbort デリゲートのコールバック メソッドを定義します。その定義は単純で、 DrawImage メソッドが null 値の callBackData パラメータで呼び出しを行うかどうかをテストするだけです。この例の本文は、次の処理を実行します。

  • DrawImageAbort コールバック メソッドのインスタンスを作成します。
  • この例が保存されているフォルダの JPEG ファイル SampImag.jpg からイメージを作成します。
  • イメージを描画するための平行四辺形を定義する点を作成します。
  • 描画するイメージの一部を選択するための四角形を作成します。
  • グラフィックス描画単位をピクセルに設定します。
  • 画面に元のイメージを描画します。
  • 調整済みイメージを描画するための追加の平行四辺形を作成します。
  • ガンマ値が通常よりも大きくなるように、調整済みイメージの属性を作成して設定します。
  • 画面に調整済みイメージを描画します。

[Visual Basic, C#] 元の未調整の平行四辺形では、その位置によって画面上のイメージの位置が決まり、四角形のサイズおよび平行四辺形のサイズと形状によって描画イメージのスケーリングと傾斜が決まります。

[Visual Basic, C#] この例では callBackData パラメータを渡すオーバーロードを使用するため、 DrawImageAbort コールバックは false を返します。これにより、 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 DrawImageParaRectAttribAbortData(e As PaintEventArgs)
' Create callback method.
Dim imageCallback As New _
Graphics.DrawImageAbort(AddressOf DrawImageCallback)
Dim imageCallbackData As Integer = 1
' Create image.
Dim newImage As Image = Image.FromFile("SampImag.jpg")
' Create parallelogram for drawing original image.
Dim ulCorner As New Point(100, 100)
Dim urCorner As New Point(550, 100)
Dim llCorner As New Point(150, 250)
Dim destPara1 As Point() =  {ulCorner, urCorner, llCorner}
' Create rectangle for source image.
Dim srcRect As New Rectangle(50, 50, 150, 150)
Dim units As GraphicsUnit = GraphicsUnit.Pixel
' Draw original image to screen.
e.Graphics.DrawImage(newImage, destPara1, srcRect, units)
' Create parallelogram for drawing adjusted image.
Dim ulCorner2 As New Point(325, 100)
Dim urCorner2 As New Point(550, 100)
Dim llCorner2 As New Point(375, 250)
Dim destPara2 As Point() =  {ulCorner2, urCorner2, llCorner2}
' Create image attributes and set large gamma.
Dim imageAttr As New ImageAttributes()
imageAttr.SetGamma(4F)
Try
' Draw image to screen.
e.Graphics.DrawImage(newImage, destPara2, srcRect, units, _
imageAttr, imageCallback, imageCallbackData)
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 DrawImageParaRectAttribAbortData(PaintEventArgs e)
{
// Create callback method.
Graphics.DrawImageAbort imageCallback
= new Graphics.DrawImageAbort(DrawImageCallback);
int imageCallbackData = 1;
// Create image.
Image newImage = Image.FromFile("SampImag.jpg");
// Create parallelogram for drawing original image.
Point ulCorner = new Point(100, 100);
Point urCorner = new Point(550, 100);
Point llCorner = new Point(150, 250);
Point[] destPara1 = {ulCorner, urCorner, llCorner};
// Create rectangle for source image.
Rectangle srcRect = new Rectangle( 50, 50, 150, 150);
GraphicsUnit units = GraphicsUnit.Pixel;
// Draw original image to screen.
e.Graphics.DrawImage(newImage, destPara1, srcRect, units);
// Create parallelogram for drawing adjusted image.
Point ulCorner2 = new Point(325, 100);
Point urCorner2 = new Point(550, 100);
Point llCorner2 = new Point(375, 250);
Point[] destPara2 = {ulCorner2, urCorner2, llCorner2};
// Create image attributes and set large gamma.
ImageAttributes imageAttr = new ImageAttributes();
imageAttr.SetGamma(4.0F);
try
{
checked
{
// Draw image to screen.
e.Graphics.DrawImage(
newImage,
destPara2,
srcRect,
units,
imageAttr,
imageCallback,
imageCallbackData);
}
}
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 オーバーロードの一覧