Scale custom picture while keeping aspect ratio

Hi, so i have a usecase where my users are uploading their logos, which i then insert into a bookmark in the header. However i never know what size the picture is, so i would like to resize the picture to a specific size.

I have tried the following but it did not keep the aspect ratio:

var picture = new Picture(document, documentBookmark.Value.Image);
picture.Layout.LockAspectRatio = true;
picture.Layout.Size = new Size(2.5, 3.5, LengthUnit.Centimeter);
bookmark.GetContent(false).Set(picture.Content);

Hi Søren,

The LockAspectRatio cannot help you with that. This option is written to the Word document and Word applications will impose that rule when trying to resize the drawing which has it.

But with GemBox.Document you can freely resize the drawing however you want.

So, try using this instead:

var picture = new Picture(document, documentBookmark.Value.Image);
var size = picture.Layout.Size;

double maxWidthInCm = 2.5;
double maxHeightInCm = 3.5;

double ratioX = maxWidthInCm / LengthUnitConverter.Convert(size.Width, LengthUnit.Point, LengthUnit.Centimeter);
double ratioY = maxHeightInCm / LengthUnitConverter.Convert(size.Height, LengthUnit.Point, LengthUnit.Centimeter);
double ratio = Math.Min(ratioX, ratioY);

if (ratio < 1)
    picture.Layout.Size = new Size(size.Width * ratio, size.Height * ratio);

bookmark.GetContent(false).Set(picture.Content);

I hope it helps.

Regards,
Mario