We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

InsertImage(filePath) not scaling correctly

I am inserting the attached A4 image into an A4 document using the documentBuilder.InsertImage(filePath) overload.

It does not scale correctly when inserted in this manner. I suspect the issue may well be dpi related but so far as I understood the overload I am calling should do all required conversions?

Hi
Thanks for your request. If you use this overload of InsertImage method then the image is inserted inline and at 100%. I think you can resize your image and then insert into the document. For example see the following code.

public void Test132()
{
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);
    // Calculate image size
    int dstWidth = (int)ConvertUtil.PointToPixel(builder.PageSetup.PageWidth - builder.PageSetup.LeftMargin - builder.PageSetup.RightMargin);
    int dstHeigth = (int)ConvertUtil.PointToPixel(builder.PageSetup.PageHeight - builder.PageSetup.TopMargin - builder.PageSetup.BottomMargin);
    // Resize image
    Image srcImage = Image.FromFile(@"Test132\in.jpg");
    Image dstImage = ResizeImage(srcImage, dstWidth, dstHeigth);
    // Insert image
    builder.InsertImage(dstImage);
    // Save document
    doc.Save(@"Test132\out.doc");
}
/// 
/// Resize source image to specifed sze
/// 
/// Source image
/// Target width
/// Target height
/// Resized image
public static Image ResizeImage(Image sourceImage, int targetWidth, int targetHeight)
{
    float ratioWidth = (float)sourceImage.Width / targetWidth;
    float ratioHeight = (float)sourceImage.Height / targetHeight;
    if (ratioWidth > ratioHeight)
        targetHeight = (int)(targetHeight * (ratioHeight / ratioWidth));
    else
    {
        if (ratioWidth < ratioHeight)
            targetWidth = (int)(targetWidth * (ratioWidth / ratioHeight));
    }
    // create target image
    Bitmap targetImage = new Bitmap(targetWidth, targetHeight, PixelFormat.Format24bppRgb);
    // set transform parameters 
    using (Graphics g = Graphics.FromImage(targetImage))
    {
        g.CompositingQuality = CompositingQuality.HighQuality;
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        // resize image
        Rectangle rc = new Rectangle(0, 0, targetImage.Width, targetImage.Height);
        g.DrawImage(sourceImage, rc);
    }
    return (Image)targetImage;
}

Also you can just set size of image. See the following code.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Calculate image size
double dstWidth = builder.PageSetup.PageWidth - builder.PageSetup.LeftMargin - builder.PageSetup.RightMargin;
double dstHeigth = builder.PageSetup.PageHeight - builder.PageSetup.TopMargin - builder.PageSetup.BottomMargin;
// Insert image
builder.InsertImage(@"Test132\in.jpg", dstWidth, dstHeigth);
// Save document
doc.Save(@"Test132\out.doc");

But in this case size of your document will be >2Mb.
Best regards.