Export (0) Print
Expand All
Expand Minimize
7 out of 14 rated this helpful - Rate this topic

Graphics.DrawImage Method

Draws the specified Image object at the specified location and with the original size.

Overload List

Draws the specified Image object, using its original physical size, at the specified location.

[Visual Basic] Overloads Public Sub DrawImage(Image, Point)
[C#] public void DrawImage(Image, Point);
[C++] public: void DrawImage(Image*, Point);
[JScript] public function DrawImage(Image, Point);

Draws the specified Image object at the specified location and with the specified shape and size.

[Visual Basic] Overloads Public Sub DrawImage(Image, Point())
[C#] public void DrawImage(Image, Point[]);
[C++] public: void DrawImage(Image*, Point[]);
[JScript] public function DrawImage(Image, Point[]);

Draws the specified Image object, using its original physical size, at the specified location.

[Visual Basic] Overloads Public Sub DrawImage(Image, PointF)
[C#] public void DrawImage(Image, PointF);
[C++] public: void DrawImage(Image*, PointF);
[JScript] public function DrawImage(Image, PointF);

Draws the specified Image object at the specified location and with the specified shape and size.

[Visual Basic] Overloads Public Sub DrawImage(Image, PointF())
[C#] public void DrawImage(Image, PointF[]);
[C++] public: void DrawImage(Image*, PointF[]);
[JScript] public function DrawImage(Image, PointF[]);

Draws the specified Image object at the specified location and with the specified size.

[Visual Basic] Overloads Public Sub DrawImage(Image, Rectangle)
[C#] public void DrawImage(Image, Rectangle);
[C++] public: void DrawImage(Image*, Rectangle);
[JScript] public function DrawImage(Image, Rectangle);

Draws the specified Image object at the specified location and with the specified size.

[Visual Basic] Overloads Public Sub DrawImage(Image, RectangleF)
[C#] public void DrawImage(Image, RectangleF);
[C++] public: void DrawImage(Image*, RectangleF);
[JScript] public function DrawImage(Image, RectangleF);

Draws the specified image, using its original physical size, at the location specified by a coordinate pair.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Sub DrawImage(Image, Integer, Integer)
[C#] public void DrawImage(Image, int, int);
[C++] public: void DrawImage(Image*, int, int);
[JScript] public function DrawImage(Image, int, int);

Draws the specified Image object, using its original physical size, at the specified location.

[Visual Basic] Overloads Public Sub DrawImage(Image, Single, Single)
[C#] public void DrawImage(Image, float, float);
[C++] public: void DrawImage(Image*, float, float);
[JScript] public function DrawImage(Image, float, float);

Draws the specified portion of the specified Image object at the specified location and with the specified size.

[Visual Basic] Overloads Public Sub DrawImage(Image, Point(), Rectangle, GraphicsUnit)
[C#] public void DrawImage(Image, Point[], Rectangle, GraphicsUnit);
[C++] public: void DrawImage(Image*, Point[], Rectangle, GraphicsUnit);
[JScript] public function DrawImage(Image, Point[], Rectangle, GraphicsUnit);

Draws the specified portion of the specified Image object at the specified location and with the specified size.

[Visual Basic] Overloads Public Sub DrawImage(Image, PointF(), RectangleF, GraphicsUnit)
[C#] public void DrawImage(Image, PointF[], RectangleF, GraphicsUnit);
[C++] public: void DrawImage(Image*, PointF[], RectangleF, GraphicsUnit);
[JScript] public function DrawImage(Image, PointF[], RectangleF, GraphicsUnit);

Draws the specified portion of the specified Image object at the specified location and with the specified size.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Sub DrawImage(Image, Rectangle, Rectangle, GraphicsUnit)
[C#] public void DrawImage(Image, Rectangle, Rectangle, GraphicsUnit);
[C++] public: void DrawImage(Image*, Rectangle, Rectangle, GraphicsUnit);
[JScript] public function DrawImage(Image, Rectangle, Rectangle, GraphicsUnit);

Draws the specified portion of the specified Image object at the specified location and with the specified size.

[Visual Basic] Overloads Public Sub DrawImage(Image, RectangleF, RectangleF, GraphicsUnit)
[C#] public void DrawImage(Image, RectangleF, RectangleF, GraphicsUnit);
[C++] public: void DrawImage(Image*, RectangleF, RectangleF, GraphicsUnit);
[JScript] public function DrawImage(Image, RectangleF, RectangleF, GraphicsUnit);

Draws the specified Image object at the specified location and with the specified size.

[Visual Basic] Overloads Public Sub DrawImage(Image, Integer, Integer, Integer, Integer)
[C#] public void DrawImage(Image, int, int, int, int);
[C++] public: void DrawImage(Image*, int, int, int, int);
[JScript] public function DrawImage(Image, int, int, int, int);

Draws a portion of an image at a specified location.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Sub DrawImage(Image, Integer, Integer, Rectangle, GraphicsUnit)
[C#] public void DrawImage(Image, int, int, Rectangle, GraphicsUnit);
[C++] public: void DrawImage(Image*, int, int, Rectangle, GraphicsUnit);
[JScript] public function DrawImage(Image, int, int, Rectangle, GraphicsUnit);

Draws the specified portion of the specified Image object at the specified location.

[Visual Basic] Overloads Public Sub DrawImage(Image, Point(), Rectangle, GraphicsUnit, ImageAttributes)
[C#] public void DrawImage(Image, Point[], Rectangle, GraphicsUnit, ImageAttributes);
[C++] public: void DrawImage(Image*, Point[], Rectangle, GraphicsUnit, ImageAttributes*);
[JScript] public function DrawImage(Image, Point[], Rectangle, GraphicsUnit, ImageAttributes);

Draws the specified portion of the specified Image object at the specified location and with the specified size.

[Visual Basic] Overloads Public Sub DrawImage(Image, PointF(), RectangleF, GraphicsUnit, ImageAttributes)
[C#] public void DrawImage(Image, PointF[], RectangleF, GraphicsUnit, ImageAttributes);
[C++] public: void DrawImage(Image*, PointF[], RectangleF, GraphicsUnit, ImageAttributes*);
[JScript] public function DrawImage(Image, PointF[], RectangleF, GraphicsUnit, ImageAttributes);

Draws a portion of an image at a specified location.

[Visual Basic] Overloads Public Sub DrawImage(Image, Single, Single, RectangleF, GraphicsUnit)
[C#] public void DrawImage(Image, float, float, RectangleF, GraphicsUnit);
[C++] public: void DrawImage(Image*, float, float, RectangleF, GraphicsUnit);
[JScript] public function DrawImage(Image, float, float, RectangleF, GraphicsUnit);

Draws the specified Image object at the specified location and with the specified size.

[Visual Basic] Overloads Public Sub DrawImage(Image, Single, Single, Single, Single)
[C#] public void DrawImage(Image, float, float, float, float);
[C++] public: void DrawImage(Image*, float, float, float, float);
[JScript] public function DrawImage(Image, float, float, float, float);

Draws the specified portion of the specified Image object at the specified location and with the specified size.

[Visual Basic] Overloads Public Sub DrawImage(Image, Point(), Rectangle, GraphicsUnit, ImageAttributes, Graphics.DrawImageAbort)
[C#] public void DrawImage(Image, Point[], Rectangle, GraphicsUnit, ImageAttributes, Graphics.DrawImageAbort);
[C++] public: void DrawImage(Image*, Point[], Rectangle, GraphicsUnit, ImageAttributes*, Graphics.DrawImageAbort*);
[JScript] public function DrawImage(Image, Point[], Rectangle, GraphicsUnit, ImageAttributes, Graphics.DrawImageAbort);

Draws the specified portion of the specified Image object at the specified location and with the specified size.

[Visual Basic] Overloads Public Sub DrawImage(Image, PointF(), RectangleF, GraphicsUnit, ImageAttributes, Graphics.DrawImageAbort)
[C#] public void DrawImage(Image, PointF[], RectangleF, GraphicsUnit, ImageAttributes, Graphics.DrawImageAbort);
[C++] public: void DrawImage(Image*, PointF[], RectangleF, GraphicsUnit, ImageAttributes*, Graphics.DrawImageAbort*);
[JScript] public function DrawImage(Image, PointF[], RectangleF, GraphicsUnit, ImageAttributes, Graphics.DrawImageAbort);

Draws the specified portion of the specified Image object at the specified location and with the specified size.

[Visual Basic] Overloads Public Sub DrawImage(Image, Point(), Rectangle, GraphicsUnit, ImageAttributes, Graphics.DrawImageAbort, Integer)
[C#] public void DrawImage(Image, Point[], Rectangle, GraphicsUnit, ImageAttributes, Graphics.DrawImageAbort, int);
[C++] public: void DrawImage(Image*, Point[], Rectangle, GraphicsUnit, ImageAttributes*, Graphics.DrawImageAbort*, int);
[JScript] public function DrawImage(Image, Point[], Rectangle, GraphicsUnit, ImageAttributes, Graphics.DrawImageAbort, int);

Draws the specified portion of the specified Image object at the specified location and with the specified size.

[Visual Basic] Overloads Public Sub DrawImage(Image, PointF(), RectangleF, GraphicsUnit, ImageAttributes, Graphics.DrawImageAbort, Integer)
[C#] public void DrawImage(Image, PointF[], RectangleF, GraphicsUnit, ImageAttributes, Graphics.DrawImageAbort, int);
[C++] public: void DrawImage(Image*, PointF[], RectangleF, GraphicsUnit, ImageAttributes*, Graphics.DrawImageAbort*, int);
[JScript] public function DrawImage(Image, PointF[], RectangleF, GraphicsUnit, ImageAttributes, Graphics.DrawImageAbort, int);

Draws the specified portion of the specified Image object at the specified location and with the specified size.

[Visual Basic] Overloads Public Sub DrawImage(Image, Rectangle, Integer, Integer, Integer, Integer, GraphicsUnit)
[C#] public void DrawImage(Image, Rectangle, int, int, int, int, GraphicsUnit);
[C++] public: void DrawImage(Image*, Rectangle, int, int, int, int, GraphicsUnit);
[JScript] public function DrawImage(Image, Rectangle, int, int, int, int, GraphicsUnit);

Draws the specified portion of the specified Image object at the specified location and with the specified size.

[Visual Basic] Overloads Public Sub DrawImage(Image, Rectangle, Single, Single, Single, Single, GraphicsUnit)
[C#] public void DrawImage(Image, Rectangle, float, float, float, float, GraphicsUnit);
[C++] public: void DrawImage(Image*, Rectangle, float, float, float, float, GraphicsUnit);
[JScript] public function DrawImage(Image, Rectangle, float, float, float, float, GraphicsUnit);

Draws the specified portion of the specified Image object at the specified location and with the specified size.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Sub DrawImage(Image, Rectangle, Integer, Integer, Integer, Integer, GraphicsUnit, ImageAttributes)
[C#] public void DrawImage(Image, Rectangle, int, int, int, int, GraphicsUnit, ImageAttributes);
[C++] public: void DrawImage(Image*, Rectangle, int, int, int, int, GraphicsUnit, ImageAttributes*);
[JScript] public function DrawImage(Image, Rectangle, int, int, int, int, GraphicsUnit, ImageAttributes);

Draws the specified portion of the specified Image object at the specified location and with the specified size.

[Visual Basic] Overloads Public Sub DrawImage(Image, Rectangle, Single, Single, Single, Single, GraphicsUnit, ImageAttributes)
[C#] public void DrawImage(Image, Rectangle, float, float, float, float, GraphicsUnit, ImageAttributes);
[C++] public: void DrawImage(Image*, Rectangle, float, float, float, float, GraphicsUnit, ImageAttributes*);
[JScript] public function DrawImage(Image, Rectangle, float, float, float, float, GraphicsUnit, ImageAttributes);

Draws the specified portion of the specified Image object at the specified location and with the specified size.

[Visual Basic] Overloads Public Sub DrawImage(Image, Rectangle, Integer, Integer, Integer, Integer, GraphicsUnit, ImageAttributes, Graphics.DrawImageAbort)
[C#] public void DrawImage(Image, Rectangle, int, int, int, int, GraphicsUnit, ImageAttributes, Graphics.DrawImageAbort);
[C++] public: void DrawImage(Image*, Rectangle, int, int, int, int, GraphicsUnit, ImageAttributes*, Graphics.DrawImageAbort*);
[JScript] public function DrawImage(Image, Rectangle, int, int, int, int, GraphicsUnit, ImageAttributes, Graphics.DrawImageAbort);

Draws the specified portion of the specified Image object at the specified location and with the specified size.

[Visual Basic] Overloads Public Sub DrawImage(Image, Rectangle, Single, Single, Single, Single, GraphicsUnit, ImageAttributes, Graphics.DrawImageAbort)
[C#] public void DrawImage(Image, Rectangle, float, float, float, float, GraphicsUnit, ImageAttributes, Graphics.DrawImageAbort);
[C++] public: void DrawImage(Image*, Rectangle, float, float, float, float, GraphicsUnit, ImageAttributes*, Graphics.DrawImageAbort*);
[JScript] public function DrawImage(Image, Rectangle, float, float, float, float, GraphicsUnit, ImageAttributes, Graphics.DrawImageAbort);

Draws the specified portion of the specified Image object at the specified location and with the specified size.

[Visual Basic] Overloads Public Sub DrawImage(Image, Rectangle, Integer, Integer, Integer, Integer, GraphicsUnit, ImageAttributes, Graphics.DrawImageAbort, IntPtr)
[C#] public void DrawImage(Image, Rectangle, int, int, int, int, GraphicsUnit, ImageAttributes, Graphics.DrawImageAbort, IntPtr);
[C++] public: void DrawImage(Image*, Rectangle, int, int, int, int, GraphicsUnit, ImageAttributes*, Graphics.DrawImageAbort*, IntPtr);
[JScript] public function DrawImage(Image, Rectangle, int, int, int, int, GraphicsUnit, ImageAttributes, Graphics.DrawImageAbort, IntPtr);

Draws the specified portion of the specified Image object at the specified location and with the specified size.

[Visual Basic] Overloads Public Sub DrawImage(Image, Rectangle, Single, Single, Single, Single, GraphicsUnit, ImageAttributes, Graphics.DrawImageAbort, IntPtr)
[C#] public void DrawImage(Image, Rectangle, float, float, float, float, GraphicsUnit, ImageAttributes, Graphics.DrawImageAbort, IntPtr);
[C++] public: void DrawImage(Image*, Rectangle, float, float, float, float, GraphicsUnit, ImageAttributes*, Graphics.DrawImageAbort*, IntPtr);
[JScript] public function DrawImage(Image, Rectangle, float, float, float, float, GraphicsUnit, ImageAttributes, Graphics.DrawImageAbort, IntPtr);

Example

[Visual Basic, C#] The following example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code first defines a callback method for the DrawImageAbort delegate; the definition is simplistic and merely tests to see whether the DrawImage method calls it with a null callBackData parameter. The main body of the example performs the following actions:

  • Creates an instance of the DrawImageAbort callback method.
  • Creates an image from a JPEG file SampImag.jpg in the folder of the example.
  • Creates points that define a destination rectangle in which to draw the image.
  • Creates a source rectangle to select the portion of the image to draw.
  • Sets the graphics drawing unit to pixel.
  • Draws the original image to the screen.
  • Creates an additional destination rectangle in which to draw an adjusted image.
  • Creates and sets the attributes of the adjusted image to have a larger-than-usual gamma value.
  • Draws the adjusted image to the screen.

[Visual Basic, C#] For the original, unadjusted destination rectangle, the position locates the image on the screen, and the size of the source rectangle and the size and shape of the destination rectangle determines the scaling of the drawn image.

[Visual Basic, C#] Because this example uses an overload that passes a callBackData parameter, the DrawImageAbort callback returns false, which causes the DrawImage method to continue, and the example draws the adjusted image to the screen.

[Visual Basic, C#] Note   This example shows how to use one of the overloaded versions of DrawImage. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
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 DrawImageRect4FloatAttribAbortData(e As PaintEventArgs)
' Create callback method.
Dim imageCallback As New _
Graphics.DrawImageAbort(AddressOf DrawImageCallback)
Dim imageCallbackData As New IntPtr(1)
' 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 Single = 50F
Dim y As Single = 50F
Dim width As Single = 150F
Dim height As Single = 150F
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)
' Draw adjusted image to screen.
Try
' Draw adjusted image to screen.
e.Graphics.DrawImage(newImage, destRect2, x, y, width, _
height, 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 DrawImageRect4FloatAttribAbortData(PaintEventArgs e)
{
// Create callback method.
Graphics.DrawImageAbort imageCallback
= new Graphics.DrawImageAbort(DrawImageCallback);
IntPtr imageCallbackData = new IntPtr(1);
// 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.
float x = 50.0F;
float y = 50.0F;
float width = 150.0F;
float height = 150.0F;
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);
// Draw adjusted image to screen.
try
{
checked
{
// Draw adjusted image to screen.
e.Graphics.DrawImage(
newImage,
destRect2,
x, y,
width, height,
units,
imageAttr,
imageCallback,
imageCallbackData);
}
}
catch (Exception ex)
{
e.Graphics.DrawString(
ex.ToString(),
new Font("Arial", 8),
Brushes.Black,
new PointF(0, 0));
}
}
        

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Graphics Class | Graphics Members | System.Drawing Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.