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