Free Support Forum - aspose.com

Width & Hight not working for Image Shape

The goal ist to add an Image/Shape directly to the object tree and setting Width and Height explicitly. This has worked before. But now, the image is always rendered in a square.
The size seems to be depending on the ordering of setting width and height.
Remark: The original code is within an IReplacingCallback, so I do modify the object tree directly

Aspose.Words 17.6.0.0
.NET Framework 4.5

[TestMethod]
public void RenderTestImage()
{
var dok = new Document();
dok.EnsureMinimum();

System.Drawing.Font font = new System.Drawing.Font("Times New Roman", 12.0f);
var img = DrawText("Width before Height", font, System.Drawing.Color.Black, System.Drawing.Color.Yellow);
var shape = new Aspose.Words.Drawing.Shape(dok, Aspose.Words.Drawing.ShapeType.Image);
//Set size of shape
shape.Width = 100; // Width before
shape.Height = 400; // Height
shape.ImageData.SetImage(img);
//Set WrapType
shape.WrapType = Aspose.Words.Drawing.WrapType.Inline;

var firstParagraph = dok.FirstSection.GetChild(NodeType.Paragraph, 0, true) as Paragraph;
firstParagraph.AppendChild(shape);
dok.Save("d:\\temp\\aaout.docx");
}

private System.Drawing.Image DrawText(String text, System.Drawing.Font font, System.Drawing.Color textColor, System.Drawing.Color backColor)
{
//first, create a dummy bitmap just to get a graphics object
System.Drawing.Image img = new System.Drawing.Bitmap(1, 1);
System.Drawing.Graphics drawing = System.Drawing.Graphics.FromImage(img);

//measure the string to see how big the image needs to be
System.Drawing.SizeF textSize = drawing.MeasureString(text, font);

//free up the dummy image and old graphics object
img.Dispose();
drawing.Dispose();

//create a new image of the right size
img = new System.Drawing.Bitmap((int)textSize.Width, (int)textSize.Height);

drawing = Graphics.FromImage(img);

//paint the background
drawing.Clear(backColor);

//create a brush for the text
Brush textBrush = new SolidBrush(textColor);

drawing.DrawString(text, font, textBrush, 0, 0);

drawing.Save();

textBrush.Dispose();
drawing.Dispose();

return img;

}
Hi Helge,

Thanks for your inquiry. Please set the value of Shape.AspectRatioLocked property to false to get the desired output. Hope this helps you.

var dok = new Document();

dok.EnsureMinimum();

System.Drawing.Font font = new System.Drawing.Font("Times New Roman", 12.0f);

var img = DrawText("Width before Height", font, System.Drawing.Color.Black, System.Drawing.Color.Yellow);

img.Save(MyDir + "output.png");

var shape = new Aspose.Words.Drawing.Shape(dok, Aspose.Words.Drawing.ShapeType.Image);

shape.AspectRatioLocked = false;

Thank you for your answer.


Setting the AspectRatioLocked property solved the problem.

According to the documentation the default value of the property should be false. The example program shows, that the default value is now true - I assume that this has been changed in one of the later versions.

//
// Summary:
// Specifies whether the shape’s aspect ratio is locked.
//
// Remarks:
// The default value is false. Has effect for top level shapes only.
public bool AspectRatioLocked {get; set; }
Hi Helge,

Thanks for your inquiry. Yes, the default value of Shape.AspectRatioLocked should be false. We have logged this problem in our issue tracking system as WORDSNET-15504. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

@hessen,

The issues you have found earlier (filed as WORDSNET-15504) have been fixed in this Aspose.Words for .NET 17.7 update and this Aspose.Words for Java 17.7 update.