Free Support Forum -

Changing Existing Image - Aspect Ratio Problem


I am using the below code to find and replace an image in an existing DOCX file using Aspose words. The original image in the DOCX file has a different width/height ratio; and the new one I am trying to use instead has different ratio. The new image is shown in the final DOCX, but the aspect ratio still belongs to the old image. Below is the code I use to replace the image:

Dim drawings As Aspose.Words.NodeCollection = gWordDocument.GetChildNodes(Aspose.Words.NodeType.DrawingML, True)
For Each drawing As Aspose.Words.Drawing.DrawingML In drawings
If drawing.HasImage Then
drawing.ImageData.ImageBytes = pLogoImageBytes
End If

Please advise on how to update/reset the image object width/height and/or scale etc. so that the final DOCX file shows the replaced image in correct aspect ratio.


Hi Coskun,

Thanks for your inquiry. Please use the DrawingML.Height and DrawingML.Width to set height and width of DrawaingML object as shown below:

<span lang=“EN-GB” style=“font-size:10.0pt;font-family:“Courier New”;mso-font-kerning:0pt;
mso-no-proof:yes”> Dim doc As New Document(“in.docx”)<o:p></o:p>

Dim builder As New DocumentBuilder(doc)

Dim shp As Shape = builder.InsertImage("C:\Aspose.jpg")

Dim Dml As DrawingML = DirectCast(doc.GetChild(NodeType.DrawingML, 0, True), DrawingML)

Dml.ImageData.ImageBytes = shp.ImageData.ImageBytes

Dml.Height = shp.Height

Dml.Width = shp.Height



I have already tried that; but doing what you have suggested ends up with more problems - shape resized but image not; which is a weird situation.

Please find the attached sample project to regenerate the issue. I am also including the sample files for your testing. Basically, there is a DOCX file, with an image inside. If you check that, the image is a portrait image; displaying RED SQUARES. When you run the test application, it simply replaces the existing image's imagedata with new image; which is a horizontal one; displaying again RED SQUARES side by side.

Please check the input.docx and sample_input.jpg so that you will understand what I mean. Basically, images have perfect sequares inside; so the final DOCX file should also display squares. Currently it stretches the image as you will notice.

Please advise.


Can you please provide information on this issue?


Hi Coskun,

Please accept my apologies for late response.

Unfortunately, Aspose.Words does not support the Aspect Ratio and Relative to original picture size options for DrawingML at the moment. However, we had already logged this feature request as WORDSNET-6874 in our issue tracking system. You will be notified via this forum thread once this feature is available.

We apologize for your inconvenience.