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?

Hi!
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");
builder.InsertImage(imageGif);
builder.InsertImage(imageJpg);
builder.InsertImage(imageJpg2);

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

Hi,

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);

doc.Save(@"350_102170_AndersJ\out.doc");

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

Hi

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.

Hi,
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

Hi

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

Best regards.

Hello,

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:

<span style=“font-size: 10pt; font-family: “Courier New”;”> private
static void
InsertImagesTest()<o:p></o:p>

{

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.MoveToDocumentStart();

Image img1 = Image.FromFile("img1.jpg");

Image img2 = Image.FromFile("img3.jpg");


//doesn't work

builder.InsertImage(img1);

builder.InsertImage(img2);

builder.Writeln();

//works

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

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

doc.Save("inserted.doc");

}

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 ?

Regards,

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

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.

Hello,

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

Regards,

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.