officefileapi-devexpress-dot-pdf-dot-pdfgraphics-dot-drawimage-x28-devexpress-dot-drawing-dot-dximage-system-dot-drawing-dot-rectanglef-x29.md
Draws an image in the specified page rectangle. The image is scaled to fit this rectangle.
Namespace : DevExpress.Pdf
Assembly : DevExpress.Pdf.v25.2.Drawing.dll
NuGet Package : DevExpress.Pdf.Drawing
public void DrawImage(
DXImage image,
RectangleF bounds
)
Public Sub DrawImage(
image As DXImage,
bounds As RectangleF
)
| Name | Type | Description |
|---|---|---|
| image | DXImage |
An image to draw.
| | bounds | RectangleF |
A page area in world coordinate system where you want to draw an image.
|
This method renders an image with its original resolution (DPI). The following formats are available:
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:
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.
The following code snippet draws an image in the page center:
using DevExpress.Pdf;
using System.Drawing;
using DevExpress.Drawing;
using (var processor = new PdfDocumentProcessor())
{
processor.LoadDocument("Documents//Document.pdf");
using (PdfGraphics graphics = processor.CreateGraphicsWorldSystem())
{
// Obtain the first document page
PdfPage page = processor.Document.Pages[0];
PdfRectangle rect = page.CropBox;
// Specify an image to draw
using (var stream = new FileStream("Documents//DevExpress.png", FileMode.Open, FileAccess.Read)) {
using (DXImage image = DXImage.FromStream(stream)) {
// 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);
}
}
}
processor.SaveDocument("result.pdf");
}
Imports DevExpress.Pdf
Imports System.Drawing
Imports DevExpress.Drawing
Using processor = 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)
Dim rect As PdfRectangle = page.CropBox
' Specify an image to draw
Using stream = New FileStream("Documents//DevExpress.png", FileMode.Open, FileAccess.Read)
Using image As DXImage = DXImage.FromStream(stream)
' Calculate the image position
Dim pageCenter As New RectangleF(CSng(rect.Width) / 2 - image.Width \ 2, CSng(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)
End Using
End Using
End Using
processor.SaveDocument("result.pdf")
End Using
The following code snippet (auto-collected from DevExpress Examples) contains a reference to the DrawImage(DXImage, 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.
pdf-document-api-replace-form-field-with-image/CS/ReplaceFormFieldWithImage/Program.cs#L68
RectangleF imageRect = new RectangleF((float)x, (float)(y - height), (float)width, (float)height);
graphics.DrawImage(image, imageRect);
}
pdf-document-api-replace-form-field-with-image/VB/ReplaceFormFieldWithImage/Program.vb#L55
Dim imageRect As RectangleF = New RectangleF(CSng(x), CSng(y - height), CSng(width), CSng(height))
graphics.DrawImage(image, imageRect)
End Sub
See Also