Back to Devexpress

PdfGraphics.DrawImage(DXImage, RectangleF, RectangleF, DXGraphicsUnit) Method

officefileapi-devexpress-dot-pdf-dot-pdfgraphics-dot-drawimage-x28-devexpress-dot-drawing-dot-dximage-system-dot-drawing-dot-rectanglef-system-dot-drawing-dot-rectanglef-devexpress-dot-drawing-dot-dxgraphicsunit-x29.md

latest6.3 KB
Original Source

PdfGraphics.DrawImage(DXImage, RectangleF, RectangleF, DXGraphicsUnit) Method

Draws the specified image part in a page rectangle.

Namespace : DevExpress.Pdf

Assembly : DevExpress.Pdf.v25.2.Drawing.dll

NuGet Package : DevExpress.Pdf.Drawing

Declaration

csharp
public void DrawImage(
    DXImage image,
    RectangleF destRect,
    RectangleF srcRect,
    DXGraphicsUnit srcUnit
)
vb
Public Sub DrawImage(
    image As DXImage,
    destRect As RectangleF,
    srcRect As RectangleF,
    srcUnit As DXGraphicsUnit
)

Parameters

NameTypeDescription
imageDXImage

An image to draw.

| | destRect | RectangleF |

A page rectangle in world coordinate system where you can draw an image. The image portion is scaled to fit the rectangle.

| | srcRect | RectangleF |

A portion of the image to draw.

| | srcUnit | DXGraphicsUnit |

Indicates the measurement units used by the srcRect parameter.

|

Remarks

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

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

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.

You can also call the PdfDocumentProcessor.OptimizeDocument method to optimize PDF document images. The method parameter specifies image compression settings.

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.AddToPageBackgroundThese methods allow you to draw content on an existing page.PdfDocumentProcessor.RenderNewPageDraws content on a new page.

Example: Draw an Image Part

The following code snippet draws a portion of an image in the specified page region. When the this part of the image is bigger than the specified region, the image is scaled to fit the region:

csharp
using DevExpress.Pdf;
using System.Drawing;
using DevExpress.Drawing;

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

        // Obtain the first document page
        PdfPage page = processor.Document.Pages[0];

        using (var stream = new FileStream("Documents//DevExpress.png", FileMode.Open, FileAccess.Read)) {
          using (DXImage image = DXImage.FromStream(stream)) {
              // Define an image part to draw:
              RectangleF imageRect = new RectangleF(0, 0, 200, 200);

              // Define an image position
              RectangleF pageRect = new RectangleF(106, 363, 100, 100);

              // Draw an image into the calculated area
              graphics.DrawImage(image, pageRect, imageRect, DXGraphicsUnit.Pixel);

              // Add graphics content to the page foreground
              graphics.AddToPageForeground(page, 72, 72);
          }
    }
    processor.SaveDocument("result.pdf");
}
vb
Imports DevExpress.Pdf
Imports System.Drawing
Imports DevExpress.Drawing

Using processor = New PdfDocumentProcessor()
  processor.LoadDocument("Documents//Document.pdf")
    ' Obtain the first document page
    Dim page As PdfPage = processor.Document.Pages(0)

    Using stream = New FileStream("Documents//DevExpress.png", FileMode.Open, FileAccess.Read)
      Using image As DXImage = DXImage.FromStream(stream)
        ' Define an image part to draw:
        Dim imageRect As New RectangleF(0, 0, 200, 200)

        ' Define an image position
        Dim pageRect As New RectangleF(106, 363, 100, 100)

        ' Draw an image into the calculated area
        graphics.DrawImage(image, pageRect, imageRect, DXGraphicsUnit.Pixel)

        ' Add graphics content to the page foreground
        graphics.AddToPageForeground(page, 72, 72)
      End Using
    End Using
  processor.SaveDocument("result.pdf")
End Using

See Also

PdfGraphics Class

PdfGraphics Members

DevExpress.Pdf Namespace