Back to Devexpress

PdfGraphics.DrawImage(Byte[], PointF) Method

officefileapi-devexpress-dot-pdf-dot-pdfgraphics-dot-drawimage-x28-system-dot-byte-system-dot-drawing-dot-pointf-x29.md

latest5.9 KB
Original Source

PdfGraphics.DrawImage(Byte[], PointF) Method

Draws the specified image data at the specified point.

Namespace : DevExpress.Pdf

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

NuGet Package : DevExpress.Pdf.Drawing

Declaration

csharp
public void DrawImage(
    byte[] data,
    PointF location
)
vb
Public Sub DrawImage(
    data As Byte(),
    location As PointF
)

Parameters

NameTypeDescription
dataByte[]

An array of bytes with image data.

| | location | PointF |

A point in world coordinate system where you can draw an image.

|

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.

To add an image to a page, call 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 in the Page Center

The following code snippet draws an image in specified point:

csharp
using DevExpress.Pdf;
using System.Drawing;

using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
{
    processor.LoadDocument("Documents//Document.pdf");
    using (PdfGraphics graphics = processor.CreateGraphicsWorldSystem())
    {
        // Obtain the first document page
        PdfPage page = processor.Document.Pages[0];

        // Specify an image to draw
        using (var image = File.ReadAllBytes("Documents//DevExpress.png"))
        {
          // Calculate the image position
          var point = new PointF((float)rect.Center.X-200, (float)rect.Center.Y);

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

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

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

    ' Specify an image to draw
    Using image = File.ReadAllBytes("Documents//DevExpress.png")
      ' Calculate the image position
      Dim point = New PointF(CSng(rect.Center.X)-200, CSng(rect.Center.Y))

      ' Draw an image into the calculated point
      graphics.DrawImage(image, point)

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

See Also

PdfGraphics Class

PdfGraphics Members

DevExpress.Pdf Namespace