When using AspPDF or AspPDF.NET to draw an image on a PDF page via the PdfCanvas.DrawImage method, the display size of the image may not be as expected. In some cases, the aspect ratio of the displayed image may be distorted as well. The following article explains how to achieve consistent and predictable display dimensions when drawing an image.
The DrawImage method takes into account not only the pixel dimensions of the image being drawn, but also its built-in resolution data as returned by the PdfImage.ResolutionX and PdfImage.ResolutionY properties. The ScaleX and ScaleY parameters (1 by default), if specified, are used to scale the image up (if greater than 1) or down (if between 0 and 1).The following formulas are used to calculate the display dimensions of an image (in user units):
Width = Image.Width * ScaleX * 72 / Image.ResolutionX
Height = Image.Height * ScaleY * 72 / Image.ResolutionYTherefore, to inscribe the image in a fixed-size rectangle, the ScaleX and ScaleY parameters must be computed based on the formulas above, and passed to the DrawImage method.
The following code snippets draw an arbitrary image with its low-left corner aligned with the low-left corner of the page (coordinates [0, 0]) and scales it in such a way that its display width is fixed to 100 user units while the height is calculated to preserve the original aspect ratio of the image:
AspPDF & ASP/VBScriptSet PDF = Server.CreateObject("Persits.PDF")
Set Doc = PDF.CreateDocument
Set Page = Doc.Pages.Add
Set Image = Doc.OpenImage("c:\path\image.jpg")' Preserve aspect ratio
Width = 200
Height = Width * Image.Height / Image.WidthScaleX = Width / Image.Width * Image.ResolutionX / 72
ScaleY = Height / Image.Height * Image.ResolutionY / 72Set Param = PDF.CreateParam
Param("X") = 0
Param("Y") = 0
Param("ScaleX") = ScaleX
Param("ScaleY") = ScaleYPage.Canvas.DrawImage Image, Param
Doc.Save "c:\path\out.pdf", False
AspPDF & .NET/C#:IPdfManager objPDF = new PdfManager();
IPdfDocument objDoc = objPDF.CreateDocument( Missing.Value );
IPdfPage objPage = objDoc.Pages.Add( Missing.Value, Missing.Value, Missing.Value );
IPdfImage objImage = objDoc.OpenImage( @"c:\path\path.jpg", Missing.Value);// Preserve aspect ratio
float fWidth = 200;
float fHeight = fWidth * objImage.Height / objImage.Width;float fScaleX = fWidth / objImage.Width * objImage.ResolutionX / 72.0f;
float fScaleY = fHeight / objImage.Height * objImage.ResolutionY / 72.0f;IPdfParam objParam = objPDF.CreateParam( Missing.Value );
objParam["X"].Value = 0;
objParam["Y"].Value = 0;
objParam["ScaleX"].Value = fScaleX;
objParam["ScaleY"].Value = fScaleY;objPage.Canvas.DrawImage( objImage, objParam );
objDoc.Save( @"c:\path\out.pdf", false );
AspPDF.NET & .NET/C#:PdfManager objPDF = new PdfManager();Keywords: ResolutionX ResolutionY DrawImage ScaleX ScaleY
PdfDocument objDoc = objPDF.CreateDocument();
PdfPage objPage = objDoc.Pages.Add();
PdfImage objImage = objDoc.OpenImage( @"c:\path\image.jpg");// Preserve aspect ratio
float fWidth = 200;
float fHeight = fWidth * objImage.Height / objImage.Width;float fScaleX = fWidth / objImage.Width * objImage.ResolutionX / 72.0f;
float fScaleY = fHeight / objImage.Height * objImage.ResolutionY / 72.0f;PdfParam objParam = objPDF.CreateParam();
objParam["X"] = 0;
objParam["Y"] = 0;
objParam["ScaleX"] = fScaleX;
objParam["ScaleY"] = fScaleY;objPage.Canvas.DrawImage( objImage, objParam );
objDoc.Save( @"c:\path\out.pdf", false );