Skip to main content
A newer version of this page is available.
.NET Framework 4.5.2+

PdfGraphics.DrawImage(Image, RectangleF) Method

Draws an image in the specified page rectangle. The image is scaled to fit this rectangle.

Namespace: DevExpress.Pdf

Assembly: DevExpress.Pdf.v21.1.Drawing.dll

NuGet Package: DevExpress.Pdf.Drawing

Declaration

public void DrawImage(
    Image image,
    RectangleF bounds
)

Parameters

Name Type Description
image Image

An image to draw.

bounds RectangleF

A page area in world coordinate system where you want to draw an image.

Remarks

This method renders an image with its original resolution (DPI). The following formats are available:

  • BMP
  • JPEG
  • PNG
  • EMF+
  • TIFF
  • GIF

The method’s dpiX and dpiY parameters do not affect image quality. They are used to convert world coordinates to page coordinates only.

If you embed a multi-page TIFF image into the document, the PdfGraphics instance draws the active frame only (the default active frame is the first frame). Use the Image.SelectActiveFrame method to select a frame to use. The compression retains for TIFF images with CCITT T.4 or CCITT T.6 compression.

You can use the following properties to reduce the image size and the size of the resulting PDF:

  • ConvertImagesToJpeg - Specifies whether to convert bitmap images into JPEG to reduce the size of the resulting PDF file.
  • JpegImageQuality - Gets or sets the quality of JPEG images in the resulting PDF file.

Important

The PdfGraphics will produce vector graphics for the EMF+ metafiles only. If a metafile contains more than GDI+ commands, it will be rasterized and rendered as a bitmap.

Tip

The PdfDocumentProcessor caches image data used as the DrawImage method parameter. If you need to draw the same image on multiple pages, you can reuse image data to improve the application performance and reduce the resulting file size.

To draw an image on the PDF page, use one of the following methods:

PdfGraphics.AddToPageForeground, PdfGraphics.AddToPageBackground
These methods allow you to draw content on an existing page.
PdfDocumentProcessor.RenderNewPage
Draws content on a new page.

The code sample below shows how to draw an image in the page center:

image at the center

using DevExpress.Pdf;
using System.Drawing;

using (var processor = new PdfDocumentProcessor())
{
    processor.LoadDocument("Documents//Document.pdf");

    using (PdfGraphics graphics = processor.CreateGraphics())
    {
        // Obtain the first document page
        PdfPage page = processor.Document.Pages[0];
        PdfRectangle rect = page.CropBox;

        // Specify an image to draw
        using (Image image = Image.FromFile("Documents//DevExpress.png"))
        {
          // Calculate the image position
          RectangleF pageCenter =
            new RectangleF((float)rect.Width / 2 - image.Width / 2, (float)rect.Height / 2 - image.Height / 2, image.Width, image.Height);

          // Draw an image into the calculated area
          graphics.DrawImage(image, pageCenter);

          // Add graphics content to the page foreground
          graphics.AddToPageForeground(page, 72, 72);
        }
    }
    processor.SaveDocument("result.pdf");
}

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the DrawImage(Image, RectangleF) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also