Graphics.DrawImage Method

Definition

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

Overloads

DrawImage(Image, Single, Single, RectangleF, GraphicsUnit)

Draws a portion of an image at a specified location.

DrawImage(Image, Effect, RectangleF, Matrix, GraphicsUnit, ImageAttributes)

Draws a portion of an image after applying a specified effect.

DrawImage(Image, Point[], Rectangle, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort)

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

DrawImage(Image, PointF[], RectangleF, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort)

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

DrawImage(Image, Point[], Rectangle, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, Int32)

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

DrawImage(Image, PointF[], RectangleF, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, Int32)

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

DrawImage(Image, Rectangle, Int32, Int32, Int32, Int32, GraphicsUnit)

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

DrawImage(Image, Rectangle, Int32, Int32, Int32, Int32, GraphicsUnit, ImageAttributes)

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

DrawImage(Image, Rectangle, Single, Single, Single, Single, GraphicsUnit, ImageAttributes)

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

DrawImage(Image, Rectangle, Int32, Int32, Int32, Int32, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort)

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

DrawImage(Image, Rectangle, Single, Single, Single, Single, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort)

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

DrawImage(Image, Rectangle, Int32, Int32, Int32, Int32, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, IntPtr)

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

DrawImage(Image, Rectangle, Single, Single, Single, Single, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, IntPtr)

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

DrawImage(Image, Rectangle, Single, Single, Single, Single, GraphicsUnit)

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

DrawImage(Image, Int32, Int32, Int32, Int32)

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

DrawImage(Image, Single, Single, Single, Single)

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

DrawImage(Image, PointF[], RectangleF, GraphicsUnit, ImageAttributes)

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

DrawImage(Image, Effect)

Draws a portion of an image after applying a specified effect.

DrawImage(Image, Point)

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

DrawImage(Image, Point[])

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

DrawImage(Image, PointF)

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

DrawImage(Image, PointF[])

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

DrawImage(Image, Rectangle)

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

DrawImage(Image, RectangleF)

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

DrawImage(Image, Int32, Int32, Rectangle, GraphicsUnit)

Draws a portion of an image at a specified location.

DrawImage(Image, Single, Single)

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

DrawImage(Image, Point[], Rectangle, GraphicsUnit)

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

DrawImage(Image, PointF[], RectangleF, GraphicsUnit)

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

DrawImage(Image, Rectangle, Rectangle, GraphicsUnit)

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

DrawImage(Image, RectangleF, RectangleF, GraphicsUnit)

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

DrawImage(Image, Int32, Int32)

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

DrawImage(Image, Point[], Rectangle, GraphicsUnit, ImageAttributes)

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

DrawImage(Image, Single, Single, RectangleF, GraphicsUnit)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

Draws a portion of an image at a specified location.

public void DrawImage (System.Drawing.Image image, float x, float y, System.Drawing.RectangleF srcRect, System.Drawing.GraphicsUnit srcUnit);

Parameters

image
Image

Image to draw.

x
Single

The x-coordinate of the upper-left corner of the drawn image.

y
Single

The y-coordinate of the upper-left corner of the drawn image.

srcRect
RectangleF

RectangleF structure that specifies the portion of the Image to draw.

srcUnit
GraphicsUnit

Member of the GraphicsUnit enumeration that specifies the units of measure used by the srcRect parameter.

Exceptions

image is null.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates an image from a JPEG file SampImag.jpg in the folder of the example.

  • Creates the coordinates at which to draw the upper-left corner of the image.

  • Creates a source rectangle from which to extract a portion of the image.

  • Sets the unit of measure of the source rectangle to pixels.

  • Draws the image to the screen.

The size of the source rectangle determines what portion of the unscaled original image is drawn to the screen.

public void DrawImage2FloatRectF(PaintEventArgs e)
{
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create coordinates for upper-left corner of image.
    float x = 100.0F;
    float y = 100.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, x, y, srcRect, units);
}

Remarks

An Image stores a value for pixel width and a value for horizontal resolution (dots per inch). The physical width, measured in inches, of an image is the pixel width divided by the horizontal resolution. For example, an image with a pixel width of 360 and a horizontal resolution of 72 dots per inch has a physical width of 5 inches. Similar remarks apply to pixel height and physical height.

This method draws a portion of an image using its physical size, so the image portion will have its correct size in inches regardless of the resolution (dots per inch) of the display device. For example, suppose an image portion has a pixel width of 216 and a horizontal resolution of 72 dots per inch. If you call this method to draw that image portion on a device that has a resolution of 96 dots per inch, the pixel width of the rendered image portion will be (216/72)*96 = 288.

See also

Applies to

.NET 9 (package-provided) וגירסאות אחרות
מוצר גירסאות
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Effect, RectangleF, Matrix, GraphicsUnit, ImageAttributes)

Source:
Graphics.cs
Source:
Graphics.cs

Draws a portion of an image after applying a specified effect.

public void DrawImage (System.Drawing.Image image, System.Drawing.Imaging.Effects.Effect effect, System.Drawing.RectangleF srcRect = default, System.Drawing.Drawing2D.Matrix? transform = default, System.Drawing.GraphicsUnit srcUnit = System.Drawing.GraphicsUnit.Pixel, System.Drawing.Imaging.ImageAttributes? imageAttr = default);

Parameters

image
Image

The Image to be drawn.

effect
Effect

The effect to be applied when drawing.

srcRect
RectangleF

The portion of the image to be drawn. Empty draws the full image.

transform
Matrix

The transform to apply to the srcRect to determine the destination.

srcUnit
GraphicsUnit

The unit of measure of the srcRect.

imageAttr
ImageAttributes

Additional adjustments to be applied, if any.

Applies to

.NET 9 (package-provided) ו- Windows Desktop 9
מוצר גירסאות
.NET 9 (package-provided)
Windows Desktop 9

DrawImage(Image, Point[], Rectangle, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

public void DrawImage (System.Drawing.Image image, System.Drawing.Point[] destPoints, System.Drawing.Rectangle srcRect, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes? imageAttr, System.Drawing.Graphics.DrawImageAbort? callback);
public void DrawImage (System.Drawing.Image image, System.Drawing.Point[] destPoints, System.Drawing.Rectangle srcRect, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes imageAttr, System.Drawing.Graphics.DrawImageAbort callback);

Parameters

image
Image

Image to draw.

destPoints
Point[]

Array of three PointF structures that define a parallelogram.

srcRect
Rectangle

Rectangle structure that specifies the portion of the image object to draw.

srcUnit
GraphicsUnit

Member of the GraphicsUnit enumeration that specifies the units of measure used by the srcRect parameter.

imageAttr
ImageAttributes

ImageAttributes that specifies recoloring and gamma information for the image object.

callback
Graphics.DrawImageAbort

Graphics.DrawImageAbort delegate that specifies a method to call during the drawing of the image. This method is called frequently to check whether to stop execution of the DrawImage(Image, Point[], Rectangle, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort) method according to application-determined criteria.

Exceptions

image is null.

Examples

The following code 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 Graphics.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 Graphics.DrawImageAbort callback method:

  • Creates an image from a JPEG file SampImag.jpg in the folder of the example.

  • Creates points that define a parallelogram in which to draw the image.

  • Creates a 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 parallelogram 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.

For the original, unadjusted parallelogram, the position locates the image on the screen, and the size of the rectangle and the size and shape of the parallelogram determines the scaling and shearing of the drawn image.

Because this example uses an overload that does not pass a callBackData parameter, the Graphics.DrawImageAbort callback returns true, which causes the DrawImage method to end, and the exception-handling code included in the example prints out the exception text rather than drawing the image.


// Define DrawImageAbort callback method.
private bool DrawImageCallback1(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;
    }
}
private void DrawImageParaRectAttribAbort(PaintEventArgs e)
{
             
    // Create callback method.
    Graphics.DrawImageAbort imageCallback
        = new Graphics.DrawImageAbort(DrawImageCallback1);
             
    // 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);
        }
    }
    catch (Exception ex)
    {
        e.Graphics.DrawString(
            ex.ToString(),
            new Font("Arial", 8),
            Brushes.Black,
            new PointF(0, 0));
    }
}

Remarks

The destPoints parameter specifies three points of a parallelogram. The three PointF structures represent the upper-left, upper-right, and lower-left corners of the parallelogram. The fourth point is extrapolated from the first three to form a parallelogram.

The srcRect parameter specifies a rectangular portion of the image object to draw. This portion is scaled and sheared to fit inside the parallelogram specified by the destPoints parameter.

This overload with the callback parameter provides the means to stop the drawing of an image once it starts according to criteria determined by the application. For example, an application could start drawing a large image and the user might scroll the image off the screen, in which case the application could stop the drawing.

See also

Applies to

.NET 9 (package-provided) וגירסאות אחרות
מוצר גירסאות
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, PointF[], RectangleF, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

public void DrawImage (System.Drawing.Image image, System.Drawing.PointF[] destPoints, System.Drawing.RectangleF srcRect, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes? imageAttr, System.Drawing.Graphics.DrawImageAbort? callback);
public void DrawImage (System.Drawing.Image image, System.Drawing.PointF[] destPoints, System.Drawing.RectangleF srcRect, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes imageAttr, System.Drawing.Graphics.DrawImageAbort callback);

Parameters

image
Image

Image to draw.

destPoints
PointF[]

Array of three PointF structures that define a parallelogram.

srcRect
RectangleF

RectangleF structure that specifies the portion of the image object to draw.

srcUnit
GraphicsUnit

Member of the GraphicsUnit enumeration that specifies the units of measure used by the srcRect parameter.

imageAttr
ImageAttributes

ImageAttributes that specifies recoloring and gamma information for the image object.

callback
Graphics.DrawImageAbort

Graphics.DrawImageAbort delegate that specifies a method to call during the drawing of the image. This method is called frequently to check whether to stop execution of the DrawImage(Image, PointF[], RectangleF, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort) method according to application-determined criteria.

Exceptions

image is null.

Examples

The following code 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 Graphics.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 Graphics.DrawImageAbort callback method.

  • Creates an image from a JPEG file SampImag.jpg in the folder of the example.

  • Creates points that define a parallelogram in which to draw the image.

  • Creates a 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 parallelogram 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.

For the original, unadjusted parallelogram, the position locates the image on the screen, and the size of the rectangle and the size and shape of the parallelogram determines the scaling and shearing of the drawn image.

Because this example uses an overload that does not pass a callBackData parameter, the Graphics.DrawImageAbort callback returns true, which causes the DrawImage method to end, and the exception-handling code included in the example prints out the exception text rather than drawing the image.


// Define DrawImageAbort callback method.
private bool DrawImageCallback3(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;
    }
}
private void DrawImageParaFRectAttribAbort(PaintEventArgs e)
{
             
    // Create callback method.
    Graphics.DrawImageAbort imageCallback
        = new Graphics.DrawImageAbort(DrawImageCallback3);
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create parallelogram for drawing original image.
    PointF ulCorner1 = new PointF(100.0F, 100.0F);
    PointF urCorner1 = new PointF(325.0F, 100.0F);
    PointF llCorner1 = new PointF(150.0F, 250.0F);
    PointF[] destPara1 = {ulCorner1, urCorner1, llCorner1};
             
    // Create rectangle for source image.
    RectangleF srcRect = new RectangleF(50.0F, 50.0F, 150.0F, 150.0F);
    GraphicsUnit units = GraphicsUnit.Pixel;
             
    // Create parallelogram for drawing adjusted image.
    PointF ulCorner2 = new PointF(325.0F, 100.0F);
    PointF urCorner2 = new PointF(550.0F, 100.0F);
    PointF llCorner2 = new PointF(375.0F, 250.0F);
    PointF[] destPara2 = {ulCorner2, urCorner2, llCorner2};
             
    // Draw original image to screen.
    e.Graphics.DrawImage(newImage, destPara1, srcRect, units);
             
    // 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,
                destPara2,
                srcRect,
                units,
                imageAttr,
                imageCallback);
        }
    }
    catch (Exception ex)
    {
        e.Graphics.DrawString(
            ex.ToString(),
            new Font("Arial", 8),
            Brushes.Black,
            new PointF(0, 0));
    }
}

Remarks

The destPoints parameter specifies three points of a parallelogram. The three PointF structures represent the upper-left, upper-right, and lower-left corners of the parallelogram. The fourth point is extrapolated from the first three to form a parallelogram.

The srcRect parameter specifies a rectangular portion of the image object to draw. This portion is scaled and sheared to fit inside the parallelogram specified by the destPoints parameter.

This overload with the callback parameter provides the means to stop the drawing of an image once it starts according to criteria determined by the application. For example, an application could start drawing a large image and the user might scroll the image off the screen, in which case the application could stop the drawing.

See also

Applies to

.NET 9 (package-provided) וגירסאות אחרות
מוצר גירסאות
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Point[], Rectangle, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, Int32)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

public void DrawImage (System.Drawing.Image image, System.Drawing.Point[] destPoints, System.Drawing.Rectangle srcRect, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes? imageAttr, System.Drawing.Graphics.DrawImageAbort? callback, int callbackData);
public void DrawImage (System.Drawing.Image image, System.Drawing.Point[] destPoints, System.Drawing.Rectangle srcRect, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes imageAttr, System.Drawing.Graphics.DrawImageAbort callback, int callbackData);

Parameters

image
Image

Image to draw.

destPoints
Point[]

Array of three PointF structures that define a parallelogram.

srcRect
Rectangle

Rectangle structure that specifies the portion of the image object to draw.

srcUnit
GraphicsUnit

Member of the GraphicsUnit enumeration that specifies the units of measure used by the srcRect parameter.

imageAttr
ImageAttributes

ImageAttributes that specifies recoloring and gamma information for the image object.

callback
Graphics.DrawImageAbort

Graphics.DrawImageAbort delegate that specifies a method to call during the drawing of the image. This method is called frequently to check whether to stop execution of the DrawImage(Image, Point[], Rectangle, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, Int32) method according to application-determined criteria.

callbackData
Int32

Value specifying additional data for the Graphics.DrawImageAbort delegate to use when checking whether to stop execution of the DrawImage(Image, Point[], Rectangle, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, Int32) method.

Examples

The following code 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 Graphics.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 Graphics.DrawImageAbort callback method.

  • Creates an image from a JPEG file SampImag.jpg in the folder of the example.

  • Creates points that define a parallelogram in which to draw the image.

  • Creates a 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 parallelogram 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.

For the original, unadjusted parallelogram, the position locates the image on the screen, and the size of the rectangle and the size and shape of the parallelogram determines the scaling and shearing of the drawn image.

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


// Define DrawImageAbort callback method.
private bool DrawImageCallback2(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;
    }
}
private void DrawImageParaRectAttribAbortData(PaintEventArgs e)
{
             
    // Create callback method.
    Graphics.DrawImageAbort imageCallback
        = new Graphics.DrawImageAbort(DrawImageCallback2);
    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));
    }
}

Remarks

The destPoints parameter specifies three points of a parallelogram. The three PointF structures represent the upper-left, upper-right, and lower-left corners of the parallelogram. The fourth point is extrapolated from the first three to form a parallelogram.

The srcRect parameter specifies a rectangular portion of the image object to draw. This portion is scaled and sheared to fit inside the parallelogram specified by the destPoints parameter.

This overload with the callback and callbackData parameters provides the means to stop the drawing of an image once it starts according to criteria and data determined by the application. For example, an application could start drawing a large image and the user might scroll the image off the screen, in which case the application could stop the drawing.

See also

Applies to

.NET 9 (package-provided) וגירסאות אחרות
מוצר גירסאות
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, PointF[], RectangleF, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, Int32)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

public void DrawImage (System.Drawing.Image image, System.Drawing.PointF[] destPoints, System.Drawing.RectangleF srcRect, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes? imageAttr, System.Drawing.Graphics.DrawImageAbort? callback, int callbackData);
public void DrawImage (System.Drawing.Image image, System.Drawing.PointF[] destPoints, System.Drawing.RectangleF srcRect, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes imageAttr, System.Drawing.Graphics.DrawImageAbort callback, int callbackData);

Parameters

image
Image

Image to draw.

destPoints
PointF[]

Array of three PointF structures that define a parallelogram.

srcRect
RectangleF

RectangleF structure that specifies the portion of the image object to draw.

srcUnit
GraphicsUnit

Member of the GraphicsUnit enumeration that specifies the units of measure used by the srcRect parameter.

imageAttr
ImageAttributes

ImageAttributes that specifies recoloring and gamma information for the image object.

callback
Graphics.DrawImageAbort

Graphics.DrawImageAbort delegate that specifies a method to call during the drawing of the image. This method is called frequently to check whether to stop execution of the DrawImage(Image, PointF[], RectangleF, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, Int32) method according to application-determined criteria.

callbackData
Int32

Value specifying additional data for the Graphics.DrawImageAbort delegate to use when checking whether to stop execution of the DrawImage(Image, PointF[], RectangleF, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, Int32) method.

Exceptions

image is null.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Pa Paint event handler. The code first defines a callback method for the Graphics.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 Graphics.DrawImageAbort callback method.

  • Creates an image from a JPEG file SampImag.jpg in the folder of the example.

  • Creates points that define a parallelogram in which to draw the image.

  • Creates a 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 parallelogram 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.

For the original, unadjusted parallelogram, the position locates the image on the screen, and the size of the rectangle and the size and shape of the parallelogram determines the scaling and shearing of the drawn image.

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


// Define DrawImageAbort callback method.
private bool DrawImageCallback4(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;
    }
}
private void DrawImageParaFRectAttribAbortData(PaintEventArgs e)
{
             
    // Create callback method.
    Graphics.DrawImageAbort imageCallback
        = new Graphics.DrawImageAbort(DrawImageCallback4);
    int imageCallbackData = 1;
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create parallelogram for drawing original image.
    PointF ulCorner1 = new PointF(100.0F, 100.0F);
    PointF urCorner1 = new PointF(325.0F, 100.0F);
    PointF llCorner1 = new PointF(150.0F, 250.0F);
    PointF[] destPara1 = {ulCorner1, urCorner1, llCorner1};
             
    // Create rectangle for source image.
    RectangleF srcRect = new RectangleF(50.0F, 50.0F, 150.0F, 150.0F);
    GraphicsUnit units = GraphicsUnit.Pixel;
             
    // Create parallelogram for drawing adjusted image.
    PointF ulCorner2 = new PointF(325.0F, 100.0F);
    PointF urCorner2 = new PointF(550.0F, 100.0F);
    PointF llCorner2 = new PointF(375.0F, 250.0F);
    PointF[] destPara2 = {ulCorner2, urCorner2, llCorner2};
             
    // Draw original image to screen.
    e.Graphics.DrawImage(newImage, destPara1, srcRect, units);
             
    // 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,
                destPara2,
                srcRect,
                units,
                imageAttr,
                imageCallback,
                imageCallbackData);
        }
    }
    catch (Exception ex)
    {
        e.Graphics.DrawString(
            ex.ToString(),
            new Font("Arial", 8),
            Brushes.Black,
            new PointF(0, 0));
    }
}

Remarks

The destPoints parameter specifies three points of a parallelogram. The three PointF structures represent the upper-left, upper-right, and lower-left corners of the parallelogram. The fourth point is extrapolated from the first three to form a parallelogram.

The srcRect parameter specifies a rectangular portion of the image object to draw. This portion is scaled and sheared to fit inside the parallelogram specified by the destPoints parameter.

This overload with the callback and callbackData parameters provides the means to stop the drawing of an image once it starts according to criteria and data determined by the application. For example, an application could start drawing a large image and the user might scroll the image off the screen, in which case the application could stop the drawing.

See also

Applies to

.NET 9 (package-provided) וגירסאות אחרות
מוצר גירסאות
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Rectangle, Int32, Int32, Int32, Int32, GraphicsUnit)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle destRect, int srcX, int srcY, int srcWidth, int srcHeight, System.Drawing.GraphicsUnit srcUnit);

Parameters

image
Image

Image to draw.

destRect
Rectangle

Rectangle structure that specifies the location and size of the drawn image. The image is scaled to fit the rectangle.

srcX
Int32

The x-coordinate of the upper-left corner of the portion of the source image to draw.

srcY
Int32

The y-coordinate of the upper-left corner of the portion of the source image to draw.

srcWidth
Int32

Width of the portion of the source image to draw.

srcHeight
Int32

Height of the portion of the source image to draw.

srcUnit
GraphicsUnit

Member of the GraphicsUnit enumeration that specifies the units of measure used to determine the source rectangle.

Exceptions

image is null.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates an image from a JPEG file SampImag.jpg in the folder of the example.

  • Creates a destination rectangle in which to draw the image.

  • Creates the coordinates of a source rectangle from which to extract a portion of the image.

  • Sets the unit of measure of the source rectangle to pixels.

  • Draws the image to the screen.

The position of the destination rectangle locates the image on the screen, and the sizes of the source and destination rectangles determine the scaling of the drawn image, and the size of the source rectangle determines what portion of the original image is drawn to the screen.

private void DrawImageRect4Int(PaintEventArgs e)
{
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create rectangle for displaying image.
    Rectangle destRect = new Rectangle(100, 100, 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 image to screen.
    e.Graphics.DrawImage(newImage, destRect, x, y, width, height, units);
}

Remarks

The srcX, srcY, srcWidth, and srcHeight parameters specify a rectangular portion, of the image object to draw. The rectangle is relative to the upper-left corner of the source image. This portion is scaled to fit inside the rectangle specified by the destRect parameter.

See also

Applies to

.NET 9 (package-provided) וגירסאות אחרות
מוצר גירסאות
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Rectangle, Int32, Int32, Int32, Int32, GraphicsUnit, ImageAttributes)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle destRect, int srcX, int srcY, int srcWidth, int srcHeight, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes? imageAttr);
public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle destRect, int srcX, int srcY, int srcWidth, int srcHeight, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes imageAttr);

Parameters

image
Image

Image to draw.

destRect
Rectangle

Rectangle structure that specifies the location and size of the drawn image. The image is scaled to fit the rectangle.

srcX
Int32

The x-coordinate of the upper-left corner of the portion of the source image to draw.

srcY
Int32

The y-coordinate of the upper-left corner of the portion of the source image to draw.

srcWidth
Int32

Width of the portion of the source image to draw.

srcHeight
Int32

Height of the portion of the source image to draw.

srcUnit
GraphicsUnit

Member of the GraphicsUnit enumeration that specifies the units of measure used to determine the source rectangle.

imageAttr
ImageAttributes

ImageAttributes that specifies recoloring and gamma information for the image object.

Exceptions

image is null.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates an image from a JPEG file SampImag.jpg in the folder of the example.

  • Creates a destination rectangle in which to draw the image.

  • Creates the coordinates of a source rectangle from which to extract a portion of the image.

  • Sets the unit of measure of the source rectangle to pixels.

  • Draws the original image to the screen.

  • Creates an additional 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.

For the original, unadjusted destination rectangle, the position locates the image on the screen, and the sizes of the source and destination rectangles determine the scaling of the drawn image, and the size of the source rectangle determines what portion of the original image is drawn to the screen.

private void DrawImageRect4IntAtrrib(PaintEventArgs e)
{
             
    // 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);
             
    // Draw adjusted image to screen.
    e.Graphics.DrawImage(newImage, destRect2, x, y, width, height, units, imageAttr);
}

Remarks

The srcX, srcY, srcWidth, and srcHeight parameters specify a rectangular portion, of the image object to draw. The rectangle is relative to the upper-left corner of the source image. This portion is scaled to fit inside the rectangle specified by the destRect parameter.

See also

Applies to

.NET 9 (package-provided) וגירסאות אחרות
מוצר גירסאות
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Rectangle, Single, Single, Single, Single, GraphicsUnit, ImageAttributes)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle destRect, float srcX, float srcY, float srcWidth, float srcHeight, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes? imageAttrs);
public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle destRect, float srcX, float srcY, float srcWidth, float srcHeight, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes imageAttrs);

Parameters

image
Image

Image to draw.

destRect
Rectangle

Rectangle structure that specifies the location and size of the drawn image. The image is scaled to fit the rectangle.

srcX
Single

The x-coordinate of the upper-left corner of the portion of the source image to draw.

srcY
Single

The y-coordinate of the upper-left corner of the portion of the source image to draw.

srcWidth
Single

Width of the portion of the source image to draw.

srcHeight
Single

Height of the portion of the source image to draw.

srcUnit
GraphicsUnit

Member of the GraphicsUnit enumeration that specifies the units of measure used to determine the source rectangle.

imageAttrs
ImageAttributes

ImageAttributes that specifies recoloring and gamma information for the image object.

Exceptions

image is null.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates an image from a JPEG file SampImag.jpg in the folder of the example.

  • Creates a destination rectangle in which to draw the image.

  • Creates the coordinates of a source rectangle from which to extract a portion of the image.

  • Sets the unit of measure of the source rectangle to pixels.

  • Draws the original image to the screen.

  • Creates an additional 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.

For the original, unadjusted destination rectangle, the position locates the image on the screen, and the sizes of the source and destination rectangles determine the scaling of the drawn image, and the size of the source rectangle determines what portion of the original image is drawn to the screen.

private void DrawImageRect4FloatAttrib(PaintEventArgs e)
{
             
    // 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.
    e.Graphics.DrawImage(newImage, destRect2, x, y, width, height, units, imageAttr);
}

Remarks

The srcX, srcY, srcWidth, and srcHeight parameters specify a rectangular portion, of the image object to draw. The rectangle is relative to the upper-left corner of the source image. This portion is scaled to fit inside the rectangle specified by the destRect parameter.

See also

Applies to

.NET 9 (package-provided) וגירסאות אחרות
מוצר גירסאות
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Rectangle, Int32, Int32, Int32, Int32, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle destRect, int srcX, int srcY, int srcWidth, int srcHeight, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes? imageAttr, System.Drawing.Graphics.DrawImageAbort? callback);
public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle destRect, int srcX, int srcY, int srcWidth, int srcHeight, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes imageAttr, System.Drawing.Graphics.DrawImageAbort callback);

Parameters

image
Image

Image to draw.

destRect
Rectangle

Rectangle structure that specifies the location and size of the drawn image. The image is scaled to fit the rectangle.

srcX
Int32

The x-coordinate of the upper-left corner of the portion of the source image to draw.

srcY
Int32

The y-coordinate of the upper-left corner of the portion of the source image to draw.

srcWidth
Int32

Width of the portion of the source image to draw.

srcHeight
Int32

Height of the portion of the source image to draw.

srcUnit
GraphicsUnit

Member of the GraphicsUnit enumeration that specifies the units of measure used to determine the source rectangle.

imageAttr
ImageAttributes

ImageAttributes that specifies recoloring and gamma information for image.

callback
Graphics.DrawImageAbort

Graphics.DrawImageAbort delegate that specifies a method to call during the drawing of the image. This method is called frequently to check whether to stop execution of the DrawImage(Image, Rectangle, Int32, Int32, Int32, Int32, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort) method according to application-determined criteria.

Exceptions

image is null.

Examples

The following code 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 Graphics.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 Graphics.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.

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.

Because this example uses an overload that does not pass a callBackData parameter, the Graphics.DrawImageAbort callback returns true, which causes the DrawImage method to end, and the exception-handling code included in the example prints out the exception text rather than drawing the image.


// Define DrawImageAbort callback method.
private bool DrawImageCallback5(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;
    }
}
private void DrawImageRect4IntAtrribAbort(PaintEventArgs e)
{
             
    // Create callback method.
    Graphics.DrawImageAbort imageCallback
        = new Graphics.DrawImageAbort(DrawImageCallback5);
             
    // 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));
    }
}

Remarks

The srcX, srcY, srcWidth, and srcHeight parameters specify a rectangular portion, of the image object to draw. The rectangle is relative to the upper-left corner of the source image. This portion is scaled to fit inside the rectangle specified by the destRect object.

This overload with the callback parameter provides the means to stop the drawing of an image once it starts according to criteria determined by the application. For example, an application could start drawing a large image and the user might scroll the image off the screen, in which case the application could stop the drawing.

See also

Applies to

.NET 9 (package-provided) וגירסאות אחרות
מוצר גירסאות
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Rectangle, Single, Single, Single, Single, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle destRect, float srcX, float srcY, float srcWidth, float srcHeight, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes? imageAttrs, System.Drawing.Graphics.DrawImageAbort? callback);
public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle destRect, float srcX, float srcY, float srcWidth, float srcHeight, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes imageAttrs, System.Drawing.Graphics.DrawImageAbort callback);

Parameters

image
Image

Image to draw.

destRect
Rectangle

Rectangle structure that specifies the location and size of the drawn image. The image is scaled to fit the rectangle.

srcX
Single

The x-coordinate of the upper-left corner of the portion of the source image to draw.

srcY
Single

The y-coordinate of the upper-left corner of the portion of the source image to draw.

srcWidth
Single

Width of the portion of the source image to draw.

srcHeight
Single

Height of the portion of the source image to draw.

srcUnit
GraphicsUnit

Member of the GraphicsUnit enumeration that specifies the units of measure used to determine the source rectangle.

imageAttrs
ImageAttributes

ImageAttributes that specifies recoloring and gamma information for the image object.

callback
Graphics.DrawImageAbort

Graphics.DrawImageAbort delegate that specifies a method to call during the drawing of the image. This method is called frequently to check whether to stop execution of the DrawImage(Image, Rectangle, Single, Single, Single, Single, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort) method according to application-determined criteria.

Exceptions

image is null.

Examples

The following code 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 Graphics.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 Graphics.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.

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.

Because this example uses an overload that does not pass a callBackData parameter, the Graphics.DrawImageAbort callback returns true, which causes the DrawImage method to end, and the exception-handling code included in the example prints out the exception text rather than drawing the image.


// Define DrawImageAbort callback method.
private bool DrawImageCallback7(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;
    }
}
private void DrawImageRect4FloatAttribAbort(PaintEventArgs e)
{
             
    // Create callback method.
    Graphics.DrawImageAbort imageCallback
        = new Graphics.DrawImageAbort(DrawImageCallback7);
             
    // 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);
    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));
    }
}

Remarks

The srcX, srcY, srcWidth, and srcHeight parameters specify a rectangular portion, of the image object to draw. The rectangle is relative to the upper-left corner of the source image. This portion is scaled to fit inside the rectangle specified by the destRect parameter.

This overload with the callback parameter provides the means to stop the drawing of an image once it starts according to criteria determined by the application. For example, an application could start drawing a large image and the user might scroll the image off the screen, in which case the application could stop the drawing.

See also

Applies to

.NET 9 (package-provided) וגירסאות אחרות
מוצר גירסאות
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Rectangle, Int32, Int32, Int32, Int32, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, IntPtr)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle destRect, int srcX, int srcY, int srcWidth, int srcHeight, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes? imageAttrs, System.Drawing.Graphics.DrawImageAbort? callback, IntPtr callbackData);
public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle destRect, int srcX, int srcY, int srcWidth, int srcHeight, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes imageAttrs, System.Drawing.Graphics.DrawImageAbort callback, IntPtr callbackData);

Parameters

image
Image

Image to draw.

destRect
Rectangle

Rectangle structure that specifies the location and size of the drawn image. The image is scaled to fit the rectangle.

srcX
Int32

The x-coordinate of the upper-left corner of the portion of the source image to draw.

srcY
Int32

The y-coordinate of the upper-left corner of the portion of the source image to draw.

srcWidth
Int32

Width of the portion of the source image to draw.

srcHeight
Int32

Height of the portion of the source image to draw.

srcUnit
GraphicsUnit

Member of the GraphicsUnit enumeration that specifies the units of measure used to determine the source rectangle.

imageAttrs
ImageAttributes

ImageAttributes that specifies recoloring and gamma information for the image object.

callback
Graphics.DrawImageAbort

Graphics.DrawImageAbort delegate that specifies a method to call during the drawing of the image. This method is called frequently to check whether to stop execution of the DrawImage(Image, Rectangle, Int32, Int32, Int32, Int32, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, IntPtr) method according to application-determined criteria.

callbackData
IntPtr

Value specifying additional data for the Graphics.DrawImageAbort delegate to use when checking whether to stop execution of the DrawImage method.

Exceptions

image is null.

Examples

The following code 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 Graphics.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 Graphics.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.

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.

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


// Define DrawImageAbort callback method.
private bool DrawImageCallback6(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;
    }
}
private void DrawImageRect4IntAtrribAbortData(PaintEventArgs e)
{
             
    // Create callback method.
    Graphics.DrawImageAbort imageCallback
        = new Graphics.DrawImageAbort(DrawImageCallback6);
    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.
    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,
                imageCallbackData);
        }
    }
    catch (Exception ex)
    {
        e.Graphics.DrawString(
            ex.ToString(),
            new Font("Arial", 8),
            Brushes.Black,
            new PointF(0, 0));
    }
}

Remarks

The srcX, srcY, srcWidth, and srcHeight parameters specify a rectangular portion, of the image object to draw. The rectangle is relative to the upper-left corner of the source image. This portion is scaled to fit inside the rectangle specified by the destRect parameter.

This overload with the callback and callbackData parameters provides the means to stop the drawing of an image once it starts according to criteria and data determined by the application. For example, an application could start drawing a large image and the user might scroll the image off the screen, in which case the application could stop the drawing.

See also

Applies to

.NET 9 (package-provided) וגירסאות אחרות
מוצר גירסאות
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Rectangle, Single, Single, Single, Single, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, IntPtr)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle destRect, float srcX, float srcY, float srcWidth, float srcHeight, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes? imageAttrs, System.Drawing.Graphics.DrawImageAbort? callback, IntPtr callbackData);
public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle destRect, float srcX, float srcY, float srcWidth, float srcHeight, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes imageAttrs, System.Drawing.Graphics.DrawImageAbort callback, IntPtr callbackData);

Parameters

image
Image

Image to draw.

destRect
Rectangle

Rectangle structure that specifies the location and size of the drawn image. The image is scaled to fit the rectangle.

srcX
Single

The x-coordinate of the upper-left corner of the portion of the source image to draw.

srcY
Single

The y-coordinate of the upper-left corner of the portion of the source image to draw.

srcWidth
Single

Width of the portion of the source image to draw.

srcHeight
Single

Height of the portion of the source image to draw.

srcUnit
GraphicsUnit

Member of the GraphicsUnit enumeration that specifies the units of measure used to determine the source rectangle.

imageAttrs
ImageAttributes

ImageAttributes that specifies recoloring and gamma information for the image object.

callback
Graphics.DrawImageAbort

Graphics.DrawImageAbort delegate that specifies a method to call during the drawing of the image. This method is called frequently to check whether to stop execution of the DrawImage(Image, Rectangle, Single, Single, Single, Single, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, IntPtr) method according to application-determined criteria.

callbackData
IntPtr

Value specifying additional data for the Graphics.DrawImageAbort delegate to use when checking whether to stop execution of the DrawImage method.

Exceptions

image is null.

Examples

The following code 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 Graphics.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 Graphics.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.

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.

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


// Define DrawImageAbort callback method.
private bool DrawImageCallback8(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(DrawImageCallback8);
    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));
    }
}

Remarks

The srcX, srcY, srcWidth, and srcHeight parameters specify a rectangular portion, of the image object to draw. The rectangle is relative to the upper-left corner of the source image. This portion is scaled to fit inside the rectangle specified by the destRect parameter.

This overload with the callback and callbackData parameters provides the means to stop the drawing of an image once it starts according to criteria and data determined by the application. For example, an application could start drawing a large image and the user might scroll the image off the screen, in which case the application could stop the drawing.

See also

Applies to

.NET 9 (package-provided) וגירסאות אחרות
מוצר גירסאות
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Rectangle, Single, Single, Single, Single, GraphicsUnit)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle destRect, float srcX, float srcY, float srcWidth, float srcHeight, System.Drawing.GraphicsUnit srcUnit);

Parameters

image
Image

Image to draw.

destRect
Rectangle

Rectangle structure that specifies the location and size of the drawn image. The image is scaled to fit the rectangle.

srcX
Single

The x-coordinate of the upper-left corner of the portion of the source image to draw.

srcY
Single

The y-coordinate of the upper-left corner of the portion of the source image to draw.

srcWidth
Single

Width of the portion of the source image to draw.

srcHeight
Single

Height of the portion of the source image to draw.

srcUnit
GraphicsUnit

Member of the GraphicsUnit enumeration that specifies the units of measure used to determine the source rectangle.

Exceptions

image is null.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates an image from a JPEG file SampImag.jpg in the folder of the example.

  • Creates the coordinates of a destination rectangle in which to draw the image.

  • Creates a source rectangle from which to extract a portion of the image.

  • Sets the unit of measure of the source rectangle to pixels.

  • Draws the image to the screen.

The position of the destination rectangle locates the image on the screen, the sizes of the source and destination rectangles determine the scaling of the drawn image, and the size of the source rectangle determines what portion of the original image is drawn to the screen.

private void DrawImageRect4Float(PaintEventArgs e)
{
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create rectangle for displaying image.
    Rectangle destRect = new Rectangle(100, 100, 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 image to screen.
    e.Graphics.DrawImage(newImage, destRect, x, y, width, height, units);
}

Remarks

The srcX, srcY, srcWidth, and srcHeight parameters specify a rectangular portion, of the image object to draw. The rectangle is relative to the upper-left corner of the source image. This portion is scaled to fit inside the rectangle specified by the destRect parameter.

See also

Applies to

.NET 9 (package-provided) וגירסאות אחרות
מוצר גירסאות
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Int32, Int32, Int32, Int32)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

public void DrawImage (System.Drawing.Image image, int x, int y, int width, int height);

Parameters

image
Image

Image to draw.

x
Int32

The x-coordinate of the upper-left corner of the drawn image.

y
Int32

The y-coordinate of the upper-left corner of the drawn image.

width
Int32

Width of the drawn image.

height
Int32

Height of the drawn image.

Exceptions

image is null.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates an image from a JPEG file SampImag.jpg in the folder of the example.

  • Creates the position and size of a rectangle in which to draw the image.

  • Draws the image to the screen.

The position of the rectangle locates the image on the screen, and the size of the original image and the size of the rectangle determines the scaling of the drawn image.

public void DrawImage4Int(PaintEventArgs e)
{
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create coordinates for upper-left corner.
             
    // of image and for size of image.
    int x = 100;
    int y = 100;
    int width = 450;
    int height = 150;
             
    // Draw image to screen.
    e.Graphics.DrawImage(newImage, x, y, width, height);
}

Remarks

The rectangle defined by the x, y, width, and height parameters determines the position and size of the drawn image.

See also

Applies to

.NET 9 (package-provided) וגירסאות אחרות
מוצר גירסאות
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Single, Single, Single, Single)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

public void DrawImage (System.Drawing.Image image, float x, float y, float width, float height);

Parameters

image
Image

Image to draw.

x
Single

The x-coordinate of the upper-left corner of the drawn image.

y
Single

The y-coordinate of the upper-left corner of the drawn image.

width
Single

Width of the drawn image.

height
Single

Height of the drawn image.

Exceptions

image is null.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates an image from a JPEG file SampImag.jpg in the folder of the example.

  • Creates the position and size of a rectangle in which to draw the image.

  • Draws the image to the screen.

The position of the rectangle locates the image on the screen, and the size of the original image and the size of the rectangle determines the scaling of the drawn image.

public void DrawImage4Float(PaintEventArgs e)
{
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create coordinates for upper-left corner.
             
    // of image and for size of image.
    float x = 100.0F;
    float y = 100.0F;
    float width = 450.0F;
    float height = 150.0F;
             
    // Draw image to screen.
    e.Graphics.DrawImage(newImage, x, y, width, height);
}

Remarks

The rectangle defined by the x, y, width, and height parameters determines the position and size of the drawn image.

See also

Applies to

.NET 9 (package-provided) וגירסאות אחרות
מוצר גירסאות
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, PointF[], RectangleF, GraphicsUnit, ImageAttributes)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

public void DrawImage (System.Drawing.Image image, System.Drawing.PointF[] destPoints, System.Drawing.RectangleF srcRect, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes? imageAttr);
public void DrawImage (System.Drawing.Image image, System.Drawing.PointF[] destPoints, System.Drawing.RectangleF srcRect, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes imageAttr);

Parameters

image
Image

Image to draw.

destPoints
PointF[]

Array of three PointF structures that define a parallelogram.

srcRect
RectangleF

RectangleF structure that specifies the portion of the image object to draw.

srcUnit
GraphicsUnit

Member of the GraphicsUnit enumeration that specifies the units of measure used by the srcRect parameter.

imageAttr
ImageAttributes

ImageAttributes that specifies recoloring and gamma information for the image object.

Exceptions

image is null.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates an image from a JPEG file SampImag.jpg in the folder of the example.

  • Creates points that define a destination parallelogram in which to draw the image.

  • Creates a source rectangle from which to extract a portion of the image.

  • Sets the unit of measure of the source rectangle to pixels.

  • Draws the original image to the screen.

  • Creates an additional parallelogram 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.

For the original, unadjusted destination parallelogram, the position locates the image on the screen, the size of the source rectangle and the size and shape of the destination parallelogram determines the scaling and shearing of the drawn image, and the size of the rectangle determines what portion of the original image is drawn to the screen.

private void DrawImageParaFRectFAttrib(PaintEventArgs e)
{
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create parallelogram for drawing original image.
    PointF ulCorner1 = new PointF(100.0F, 100.0F);
    PointF urCorner1 = new PointF(325.0F, 100.0F);
    PointF llCorner1 = new PointF(150.0F, 250.0F);
    PointF[] destPara1 = {ulCorner1, urCorner1, llCorner1};
             
    // Create rectangle for source image.
    RectangleF srcRect = new RectangleF(50.0F, 50.0F, 150.0F, 150.0F);
    GraphicsUnit units = GraphicsUnit.Pixel;
             
    // Create parallelogram for drawing adjusted image.
    PointF ulCorner2 = new PointF(325.0F, 100.0F);
    PointF urCorner2 = new PointF(550.0F, 100.0F);
    PointF llCorner2 = new PointF(375.0F, 250.0F);
    PointF[] destPara2 = {ulCorner2, urCorner2, llCorner2};
             
    // Draw original image to screen.
    e.Graphics.DrawImage(newImage, destPara1, srcRect, units);
             
    // Create image attributes and set large gamma.
    ImageAttributes imageAttr = new ImageAttributes();
    imageAttr.SetGamma(4.0F);
             
    // Draw adjusted image to screen.
    e.Graphics.DrawImage(newImage, destPara2, srcRect, units, imageAttr);
}

Remarks

The destPoints parameter specifies three points of a parallelogram. The three PointF structures represent the upper-left, upper-right, and lower-left corners of the parallelogram. The fourth point is extrapolated from the first three to form a parallelogram.

The srcRect parameter specifies a rectangular portion of the image object to draw. This portion is scaled and sheared to fit inside the parallelogram specified by the destPoints parameter.

See also

Applies to

.NET 9 (package-provided) וגירסאות אחרות
מוצר גירסאות
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Effect)

Source:
Graphics.cs
Source:
Graphics.cs

Draws a portion of an image after applying a specified effect.

public void DrawImage (System.Drawing.Image image, System.Drawing.Imaging.Effects.Effect effect);

Parameters

image
Image

The Image to be drawn.

effect
Effect

The effect to be applied when drawing.

Applies to

.NET 9 (package-provided) ו- Windows Desktop 9
מוצר גירסאות
.NET 9 (package-provided)
Windows Desktop 9

DrawImage(Image, Point)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

public void DrawImage (System.Drawing.Image image, System.Drawing.Point point);

Parameters

image
Image

Image to draw.

point
Point

Point structure that represents the location of the upper-left corner of the drawn image.

Exceptions

image is null.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates an image from a JPEG file SampImag.jpg in the folder of the example.

  • Creates a point at which to draw the upper-left corner of the image.

  • Draws the unscaled image to the screen.

private void DrawImagePoint(PaintEventArgs e)
{         
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create Point for upper-left corner of image.
    Point ulCorner = new Point(100, 100);
             
    // Draw image to screen.
    e.Graphics.DrawImage(newImage, ulCorner);
}

Remarks

An Image stores a value for pixel width and a value for horizontal resolution (dots per inch). The physical width, measured in inches, of an image is the pixel width divided by the horizontal resolution. For example, an image with a pixel width of 216 and a horizontal resolution of 72 dots per inch has a physical width of 3 inches. Similar remarks apply to pixel height and physical height.

This method draws an image using its physical size, so the image will have its correct size in inches regardless of the resolution (dots per inch) of the display device. For example, suppose an image has a pixel width of 216 and a horizontal resolution of 72 dots per inch. If you call this method to draw that image on a device that has a resolution of 96 dots per inch, the pixel width of the rendered image will be (216/72)*96 = 288.

See also

Applies to

.NET 9 (package-provided) וגירסאות אחרות
מוצר גירסאות
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Point[])

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

public void DrawImage (System.Drawing.Image image, System.Drawing.Point[] destPoints);

Parameters

image
Image

Image to draw.

destPoints
Point[]

Array of three Point structures that define a parallelogram.

Exceptions

image is null.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates an image from a JPEG file SampImag.jpg in the folder of the example.

  • Creates points that define a parallelogram in which to draw the image.

  • Draws the image to the screen.

The position of the parallelogram locates the image on the screen, and the size of the original image and the size and shape of the parallelogram determines the scaling and shearing of the drawn image.

private void DrawImagePara(PaintEventArgs e)
{
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create parallelogram for drawing image.
    Point ulCorner = new Point(100, 100);
    Point urCorner = new Point(550, 100);
    Point llCorner = new Point(150, 250);
    Point[] destPara = {ulCorner, urCorner, llCorner};
             
    // Draw image to screen.
    e.Graphics.DrawImage(newImage, destPara);
}

Remarks

The destPoints parameter specifies three points of a parallelogram. The three Point structures represent the upper-left, upper-right, and lower-left corners of the parallelogram. The fourth point is extrapolated from the first three to form a parallelogram.

The image represented by the image parameter is scaled and sheared to fit the shape of the parallelogram specified by the destPoints parameters.

See also

Applies to

.NET 9 (package-provided) וגירסאות אחרות
מוצר גירסאות
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, PointF)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

public void DrawImage (System.Drawing.Image image, System.Drawing.PointF point);

Parameters

image
Image

Image to draw.

point
PointF

PointF structure that represents the upper-left corner of the drawn image.

Exceptions

image is null.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates an image from a JPEG file SampImag.jpg in the folder of the example.

  • Creates a point at which to draw the upper-left corner of the image.

  • Draws the unscaled image to the screen.

private void DrawImagePointF(PaintEventArgs e)
{
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create point for upper-left corner of image.
    PointF ulCorner = new PointF(100.0F, 100.0F);
             
    // Draw image to screen.
    e.Graphics.DrawImage(newImage, ulCorner);
}

Remarks

An Image stores a value for pixel width and a value for horizontal resolution (dots per inch). The physical width, measured in inches, of an image is the pixel width divided by the horizontal resolution. For example, an image with a pixel width of 216 and a horizontal resolution of 72 dots per inch has a physical width of 3 inches. Similar remarks apply to pixel height and physical height.

This method draws an image using its physical size, so the image will have its correct size in inches regardless of the resolution (dots per inch) of the display device. For example, suppose an image has a pixel width of 216 and a horizontal resolution of 72 dots per inch. If you call this method to draw that image on a device that has a resolution of 96 dots per inch, the pixel width of the rendered image will be (216/72)*96 = 288.

See also

Applies to

.NET 9 (package-provided) וגירסאות אחרות
מוצר גירסאות
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, PointF[])

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

public void DrawImage (System.Drawing.Image image, System.Drawing.PointF[] destPoints);

Parameters

image
Image

Image to draw.

destPoints
PointF[]

Array of three PointF structures that define a parallelogram.

Exceptions

image is null.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates an image from a JPEG file SampImag.jpg in the folder of the example.

  • Creates points that define a parallelogram in which to draw the image.

  • Draws the image to the screen.

The position of the parallelogram locates the image on the screen, and the size of the original image and the size and shape of the parallelogram determines the scaling and shearing of the drawn image.

private void DrawImageParaF(PaintEventArgs e)
{
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create parallelogram for drawing image.
    PointF ulCorner = new PointF(100.0F, 100.0F);
    PointF urCorner = new PointF(550.0F, 100.0F);
    PointF llCorner = new PointF(150.0F, 250.0F);
    PointF[] destPara = {ulCorner, urCorner, llCorner};
             
    // Draw image to screen.
    e.Graphics.DrawImage(newImage, destPara);
}

Remarks

The destPoints parameter specifies three points of a parallelogram. The three PointF structures represent the upper-left, upper-right, and lower-left corners of the parallelogram. The fourth point is extrapolated from the first three to form a parallelogram.

The image represented by the image object is scaled and sheared to fit the shape of the parallelogram specified by the destPoints parameter.

See also

Applies to

.NET 9 (package-provided) וגירסאות אחרות
מוצר גירסאות
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Rectangle)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle rect);

Parameters

image
Image

Image to draw.

rect
Rectangle

Rectangle structure that specifies the location and size of the drawn image.

Exceptions

image is null.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates an image from a JPEG file SampImag.jpg in the folder of the example.

  • Creates a rectangle in which to draw the image.

  • Draws the image to the screen.

The position of the rectangle locates the image on the screen, and the size of the original image and the size of the rectangle determines the scaling of the drawn image.

private void DrawImageRect(PaintEventArgs e)
{
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create rectangle for displaying image.
    Rectangle destRect = new Rectangle(100, 100, 450, 150);
             
    // Draw image to screen.
    e.Graphics.DrawImage(newImage, destRect);
}

Remarks

The image represented by the image object is scaled to the dimensions of the rect rectangle.

See also

Applies to

.NET 9 (package-provided) וגירסאות אחרות
מוצר גירסאות
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, RectangleF)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

public void DrawImage (System.Drawing.Image image, System.Drawing.RectangleF rect);

Parameters

image
Image

Image to draw.

rect
RectangleF

RectangleF structure that specifies the location and size of the drawn image.

Exceptions

image is null.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates an image from a JPEG file SampImag.jpg in the folder of the example.

  • Creates a rectangle in which to draw the image.

  • Draws the image to the screen.

The position of the rectangle locates the image on the screen, and the original size of the image and the size of the rectangle determines the scaling of the drawn image.

public void DrawImageRectF(PaintEventArgs e)
{
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create rectangle for displaying image.
    RectangleF rect = new RectangleF(100.0F, 100.0F, 450.0F, 150.0F);
             
    // Draw image to screen.
    e.Graphics.DrawImage(newImage, rect);
}

Remarks

The image represented by the image object is scaled to the dimensions of the rect rectangle.

See also

Applies to

.NET 9 (package-provided) וגירסאות אחרות
מוצר גירסאות
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Int32, Int32, Rectangle, GraphicsUnit)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

Draws a portion of an image at a specified location.

public void DrawImage (System.Drawing.Image image, int x, int y, System.Drawing.Rectangle srcRect, System.Drawing.GraphicsUnit srcUnit);

Parameters

image
Image

Image to draw.

x
Int32

The x-coordinate of the upper-left corner of the drawn image.

y
Int32

The y-coordinate of the upper-left corner of the drawn image.

srcRect
Rectangle

Rectangle structure that specifies the portion of the image object to draw.

srcUnit
GraphicsUnit

Member of the GraphicsUnit enumeration that specifies the units of measure used by the srcRect parameter.

Exceptions

image is null.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates an image from a JPEG file SampImag.jpg in the folder of the example.

  • Creates the coordinates at which to draw the upper-left corner of the image.

  • Creates a source rectangle from which to extract a portion of the image.

  • Sets the unit of measure of the source rectangle to pixels.

  • Draws the image to the screen.

The size of the source rectangle determines what portion of the unscaled original image is drawn to the screen.

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);
}

Remarks

An Image stores a value for pixel width and a value for horizontal resolution (dots per inch). The physical width, measured in inches, of an image is the pixel width divided by the horizontal resolution. For example, an image with a pixel width of 360 and a horizontal resolution of 72 dots per inch has a physical width of 5 inches. Similar remarks apply to pixel height and physical height.

This method draws a portion of an image using its physical size, so the image portion will have its correct size in inches regardless of the resolution (dots per inch) of the display device. For example, suppose an image portion has a pixel width of 216 and a horizontal resolution of 72 dots per inch. If you call this method to draw that image portion on a device that has a resolution of 96 dots per inch, the pixel width of the rendered image portion will be (216/72)*96 = 288.

See also

Applies to

.NET 9 (package-provided) וגירסאות אחרות
מוצר גירסאות
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Single, Single)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

public void DrawImage (System.Drawing.Image image, float x, float y);

Parameters

image
Image

Image to draw.

x
Single

The x-coordinate of the upper-left corner of the drawn image.

y
Single

The y-coordinate of the upper-left corner of the drawn image.

Exceptions

image is null.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates an image from a JPEG file SampImag.jpg in the folder of the example.

  • Creates the coordinates of a point at which to draw the upper-left corner of the image.

  • Draws the unscaled image to the screen.

public void DrawImage2Float(PaintEventArgs e)
{
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create coordinates for upper-left corner of image.
    float x = 100.0F;
    float y = 100.0F;
             
    // Draw image to screen.
    e.Graphics.DrawImage(newImage, x, y);
}

Remarks

An Image stores a value for pixel width and a value for horizontal resolution (dots per inch). The physical width, measured in inches, of an image is the pixel width divided by the horizontal resolution. For example, an image with a pixel width of 216 and a horizontal resolution of 72 dots per inch has a physical width of 3 inches. Similar remarks apply to pixel height and physical height.

This method draws an image using its physical size, so the image will have its correct size in inches regardless of the resolution (dots per inch) of the display device. For example, suppose an image has a pixel width of 216 and a horizontal resolution of 72 dots per inch. If you call this method to draw that image on a device that has a resolution of 96 dots per inch, the pixel width of the rendered image will be (216/72)*96 = 288.

See also

Applies to

.NET 9 (package-provided) וגירסאות אחרות
מוצר גירסאות
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Point[], Rectangle, GraphicsUnit)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

public void DrawImage (System.Drawing.Image image, System.Drawing.Point[] destPoints, System.Drawing.Rectangle srcRect, System.Drawing.GraphicsUnit srcUnit);

Parameters

image
Image

Image to draw.

destPoints
Point[]

Array of three Point structures that define a parallelogram.

srcRect
Rectangle

Rectangle structure that specifies the portion of the image object to draw.

srcUnit
GraphicsUnit

Member of the GraphicsUnit enumeration that specifies the units of measure used by the srcRect parameter.

Exceptions

image is null.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates an image from a JPEG file SampImag.jpg in the folder of the example.

  • Creates points that define a parallelogram in which to draw the image.

  • Creates a rectangle to select the portion of the image to draw.

  • Sets the graphics drawing unit to pixel.

  • Draws the image to the screen.

The position of the parallelogram locates the image on the screen, and the size of the rectangle and the size and shape of the parallelogram determines the scaling and shearing of the drawn image.

private void DrawImageParaRect(PaintEventArgs e)
{
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create parallelogram for drawing image.
    Point ulCorner = new Point(100, 100);
    Point urCorner = new Point(325, 100);
    Point llCorner = new Point(150, 250);
    Point[] destPara = {ulCorner, urCorner, llCorner};
             
    // 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, destPara, srcRect, units);
}

Remarks

The destPoints parameter specifies three points of a parallelogram. The three Point structures represent the upper-left, upper-right, and lower-left corners of the parallelogram. The fourth point is extrapolated from the first three to form a parallelogram.

The srcRect parameter specifies a rectangular portion of the image object to draw. This portion is scaled and sheared to fit inside the parallelogram specified by the destPoints parameter.

See also

Applies to

.NET 9 (package-provided) וגירסאות אחרות
מוצר גירסאות
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, PointF[], RectangleF, GraphicsUnit)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

public void DrawImage (System.Drawing.Image image, System.Drawing.PointF[] destPoints, System.Drawing.RectangleF srcRect, System.Drawing.GraphicsUnit srcUnit);

Parameters

image
Image

Image to draw.

destPoints
PointF[]

Array of three PointF structures that define a parallelogram.

srcRect
RectangleF

RectangleF structure that specifies the portion of the image object to draw.

srcUnit
GraphicsUnit

Member of the GraphicsUnit enumeration that specifies the units of measure used by the srcRect parameter.

Exceptions

image is null.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates an image from a JPEG file SampImag.jpg in the folder of the example.

  • Creates points that define a destination parallelogram in which to draw the image.

  • Creates a source rectangle from which to extract a portion of the image.

  • Sets the unit of measure of the source rectangle to pixels.

  • Draws the image to the screen.

The position of the destination parallelogram locates the image on the screen, the size of the source rectangle and the size and shape of the destination parallelogram determines the scaling and shearing of the drawn image, and the size of the rectangle determines what portion of the original image is drawn to the screen.

private void DrawImageParaFRectF(PaintEventArgs e)
{
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create parallelogram for drawing image.
    PointF ulCorner = new PointF(100.0F, 100.0F);
    PointF urCorner = new PointF(550.0F, 100.0F);
    PointF llCorner = new PointF(150.0F, 250.0F);
    PointF[] destPara = {ulCorner, urCorner, llCorner};
             
    // 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, destPara, srcRect, units);
}

Remarks

The destPoints parameter specifies three points of a parallelogram. The three PointF structures represent the upper-left, upper-right, and lower-left corners of the parallelogram. The fourth point is extrapolated from the first three to form a parallelogram.

The srcRect parameter specifies a rectangular portion of the image object to draw. This portion is scaled and sheared to fit inside the parallelogram specified by the destPoints parameter.

See also

Applies to

.NET 9 (package-provided) וגירסאות אחרות
מוצר גירסאות
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Rectangle, Rectangle, GraphicsUnit)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle destRect, System.Drawing.Rectangle srcRect, System.Drawing.GraphicsUnit srcUnit);

Parameters

image
Image

Image to draw.

destRect
Rectangle

Rectangle structure that specifies the location and size of the drawn image. The image is scaled to fit the rectangle.

srcRect
Rectangle

Rectangle structure that specifies the portion of the image object to draw.

srcUnit
GraphicsUnit

Member of the GraphicsUnit enumeration that specifies the units of measure used by the srcRect parameter.

Exceptions

image is null.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates an image from a JPEG file SampImag.jpg in the folder of the example.

  • Creates a destination rectangle in which to draw the image.

  • Creates a source rectangle from which to extract a portion of the image.

  • Sets the unit of measure of the source rectangle to pixels.

  • Draws the image to the screen.

The position of the destination rectangle locates the image on the screen, the sizes of the source and destination rectangles determine the scaling of the drawn image, and the size of the source rectangle determines what portion of the original image is drawn to the screen.

private void DrawImageRectRect(PaintEventArgs e)
{
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create rectangle for displaying image.
    Rectangle destRect = new Rectangle(100, 100, 450, 150);
             
    // 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, destRect, srcRect, units);
}

Remarks

The srcRect parameter specifies a rectangular portion of the image object to draw. This portion is scaled to fit inside the rectangle specified by the destRect parameter.

See also

Applies to

.NET 9 (package-provided) וגירסאות אחרות
מוצר גירסאות
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, RectangleF, RectangleF, GraphicsUnit)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

public void DrawImage (System.Drawing.Image image, System.Drawing.RectangleF destRect, System.Drawing.RectangleF srcRect, System.Drawing.GraphicsUnit srcUnit);

Parameters

image
Image

Image to draw.

destRect
RectangleF

RectangleF structure that specifies the location and size of the drawn image. The image is scaled to fit the rectangle.

srcRect
RectangleF

RectangleF structure that specifies the portion of the image object to draw.

srcUnit
GraphicsUnit

Member of the GraphicsUnit enumeration that specifies the units of measure used by the srcRect parameter.

Exceptions

image is null.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates an image from a JPEG file SampImag.jpg in the folder of the example.

  • Creates a destination rectangle in which to draw the image.

  • Creates a source rectangle from which to extract a portion of the image.

  • Sets the unit of measure of the source rectangle to pixels.

  • Draws the image to the screen.

The position of the destination rectangle locates the image on the screen, the sizes of the source and destination rectangles determine the scaling of the drawn image, and the size of the source rectangle determines what portion of the original image is drawn to the screen.

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);
}

Remarks

The srcRect parameter specifies a rectangular portion of the image object to draw. This portion is scaled to fit inside the rectangle specified by the destRect parameter.

See also

Applies to

.NET 9 (package-provided) וגירסאות אחרות
מוצר גירסאות
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Int32, Int32)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

public void DrawImage (System.Drawing.Image image, int x, int y);

Parameters

image
Image

Image to draw.

x
Int32

The x-coordinate of the upper-left corner of the drawn image.

y
Int32

The y-coordinate of the upper-left corner of the drawn image.

Exceptions

image is null.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates an image from a JPEG file SampImag.jpg in the folder of the example SampImag.jpg in the folder of the example.

  • Creates the coordinates of a point at which to draw the upper-left corner of the image.

  • Draws the unscaled image.

public void DrawImage2Int(PaintEventArgs e)
{
                
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
                
    // Create coordinates for upper-left corner of image.
    int x = 100;
    int y = 100;
                
    // Draw image to screen.
    e.Graphics.DrawImage(newImage, x, y);
}

Remarks

An Image stores a value for pixel width and a value for horizontal resolution (dots per inch). The physical width, measured in inches, of an image is the pixel width divided by the horizontal resolution. For example, an image with a pixel width of 216 and a horizontal resolution of 72 dots per inch has a physical width of 3 inches. Similar remarks apply to pixel height and physical height.

The DrawImage method draws an image using its physical size, so the image will have its correct size in inches regardless of the resolution (dots per inch) of the display device. For example, suppose an image has a pixel width of 216 and a horizontal resolution of 72 dots per inch. If you call DrawImage to draw that image on a device that has a resolution of 96 dots per inch, the pixel width of the rendered image will be (216/72)*96 = 288.

See also

Applies to

.NET 9 (package-provided) וגירסאות אחרות
מוצר גירסאות
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Point[], Rectangle, GraphicsUnit, ImageAttributes)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

public void DrawImage (System.Drawing.Image image, System.Drawing.Point[] destPoints, System.Drawing.Rectangle srcRect, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes? imageAttr);
public void DrawImage (System.Drawing.Image image, System.Drawing.Point[] destPoints, System.Drawing.Rectangle srcRect, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes imageAttr);

Parameters

image
Image

Image to draw.

destPoints
Point[]

Array of three Point structures that define a parallelogram.

srcRect
Rectangle

Rectangle structure that specifies the portion of the image object to draw.

srcUnit
GraphicsUnit

Member of the GraphicsUnit enumeration that specifies the units of measure used by the srcRect parameter.

imageAttr
ImageAttributes

ImageAttributes that specifies recoloring and gamma information for the image object.

Exceptions

image is null.

Examples

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates an image from a JPEG file SampImag.jpg in the folder of the example.

  • Creates points that define a parallelogram in which to draw the image.

  • Creates a 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 parallelogram 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.

For the original, unadjusted parallelogram, the position locates the image on the screen, and the size of the rectangle and the size and shape of the parallelogram determines the scaling and shearing of the drawn image.

private void DrawImageParaRectAttrib(PaintEventArgs e)
{
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create parallelogram for drawing image.
    Point ulCorner1 = new Point(100, 100);
    Point urCorner1 = new Point(325, 100);
    Point llCorner1 = new Point(150, 250);
    Point[] destPara1 = {ulCorner1, urCorner1, llCorner1};
             
    // 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);
             
    // Draw adjusted image to screen.
    e.Graphics.DrawImage(newImage, destPara2, srcRect, units, imageAttr);
}

Remarks

The destPoints parameter specifies three points of a parallelogram. The three Point structures represent the upper-left, upper-right, and lower-left corners of the parallelogram. The fourth point is extrapolated from the first three to form a parallelogram.

The srcRect parameter specifies a rectangular portion of theimage object to draw. This portion is scaled and sheared to fit inside the parallelogram specified by the destPoints parameter.

See also

Applies to

.NET 9 (package-provided) וגירסאות אחרות
מוצר גירסאות
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9