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

Free Support Forum - aspose.com

How to get 100% imagesize when inserting images with DocumentBuilder.InsertImage?

I can’t figure out how to make inserted images display at 100% of their original size.
The following code produces 3 different image sizes in word even though they have the exact same size in pixels (320*426).

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
System.Drawing.Image imageGif = System.Drawing.Image.FromFile(Server.MapPath("/") + "test.gif");
System.Drawing.Image imageJpg = System.Drawing.Image.FromFile(Server.MapPath("/") + "test.jpg");
System.Drawing.Image imageJpg2 = System.Drawing.Image.FromFile(Server.MapPath("/") + "test2.jpg");

test.gif gets correct size, test.jpg is too large and test2.jpg is too small.
I have attached the images and resulting word doc.
Thanks, Anders

Thank you for reporting to us. I managed to reproduce the problem. I created an issue #4027 in our defects database and we will investigate whether and when it can be fixed. Please expect a reply before the next hotfix (within 2-3 weeks).
You can use workaround. See the following code.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Image imgGif = Image.FromFile(@"350_102170_AndersJ\test.gif");
Image imgJpg = Image.FromFile(@"350_102170_AndersJ\test.JPG");
Image imgJpg2 = Image.FromFile(@"350_102170_AndersJ\test2.JPG");
builder.InsertImage(imgGif, imgGif.Width * 0.75, imgGif.Height * 0.75);
builder.InsertImage(imgJpg, imgJpg.Width * 0.75, imgJpg.Height * 0.75);
builder.InsertImage(imgJpg2, imgJpg2.Width * 0.75, imgJpg2.Height * 0.75);

I hope that this will help you.
Best regards.

Thanks for the workaround and your quick response. I will probably use this workaround method.
It has a small problem however (which you may already know about): Although the images get displayed with correct dimensions the (jpg) images are actually scaled to 188%, thereby losing a bit of quality.
FYI: The difference I know of between test.jpg and test2.jpg is in their resolution.
Best regards, Anders

Thank you for your remark. I get size of image programmatically. And I believe this size is correct. But MSWord show another size when you insert this image into the document.
test2.jpg has size 320x426
But MSWord shows original size as 171x227
Best regards.

Yes this is consistent with my results.
I don’t know if this helps but I did a little experimenting and found something odd:
If I use the Shape object when inserting images:

Aspose.Words.Drawing.Shape shapeGif = builder.InsertImage(imageGif);
Aspose.Words.Drawing.Shape shapeJpg = builder.InsertImage(imageJpg);

… and set a watch on shapeGif.ImageData.ImageSize I see that HeightInPoints = HeightInPixels * 0.75 (which I excpected) whereas for the jpg HeightInPixels = HeightInPoints.
Best regards, Anders

Thank you for your investigation. This will help us to understand this problem and solve it.
Best regards.


I think I’ve got a similar problem. I also want to insert images with their correct size but cannot do so.
You’ll find attached the two images I’m trying to insert.

Here the code I use:

private static void InsertImagesTest()
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);
    Image img1 = Image.FromFile("img1.jpg");
    Image img2 = Image.FromFile("img3.jpg");

    // doesn't work
    // works
    builder.InsertImage(img1, ConvertUtil.PixelToPoint(img1.Width), ConvertUtil.PixelToPoint(img1.Height));
    builder.InsertImage(img2, ConvertUtil.PixelToPoint(img2.Width), ConvertUtil.PixelToPoint(img2.Height));

If I insert these images using MS Word, I get the same incorrect sizes.
Is this a bug or a functionality of MS Word I don’t understand ?


Thanks for your request. This occurs, because your images have different resolution. The first image has resolution 300dpi, and the second one – 150dpi. For example try using the following code:

builder.InsertImage(img1, ConvertUtil.PixelToPoint(img1.Width, img1.HorizontalResolution),
ConvertUtil.PixelToPoint(img1.Height, img1.VerticalResolution));
builder.InsertImage(img2, ConvertUtil.PixelToPoint(img2.Width, img2.HorizontalResolution),
ConvertUtil.PixelToPoint(img2.Height, img2.VerticalResolution));

You will see that in this case, Aspose.Words inserts images as MS Word does, because you considered the influence of resolution of images.
Best regards.


Thank you very much for your answer. I now get it.


The issues you have found earlier (filed as WORDSNET-1445) have been fixed in this .NET update and in this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.