Insert a photo with the aspect ratio fixed

I would like to insert a photo with the aspect ratio fixed, but it doesn’t work.

After setting RelativeToOriginalPictureSize、IsLockAspectRatio = true , I set the width or height, but the proportions are not maintained.

I would like to set the largest image within the frame while preserving the aspect ratio, similar to how Excel works. Please tell me how to call the API.

Picture status after the program ends.
image.png (25.2 KB)

Expected results
image.png (25.6 KB)

Test Source
AsposeCellsTestA.7z (1.9 KB)

@ymd,

Thank you for providing the sample images, screenshots, and code segment.

Could you also please share the following (please zip the files before attaching):

  1. Your current output Excel file created with Aspose.Cells that shows undesired results where the size and other proportions are not maintained.
  2. A sample Excel file which demonstrates your expected results. You may create the Excel file with the desired results manually in MS Excel.

We will evaluate and look into your issue soon.

Link the file output by the program and the file with the expected result.

MyBooks.7z (17.6 KB)

@ymd,

Thanks for the sample files.

Please set scale height and scale width for both pictures accordingly. See the following lines of code (in bold) that you may add to your code segment to get your expected results:

picA.WidthPt = rangeA.Width;

picA.HeightScale = 54;
picA.WidthScale = 53;

picB.HeightPt = rangeB.Height;

picB.HeightScale = 40;
picB.WidthScale = 40;

Hope, this helps a bit.

As a result of executing the correction after following the advice, the appearance was corrected, but the set value and the displayed value are different.

picA.HeightScale = 54;
picA.WidthScale = 53;

Scale Height : 56%
Scale Width : 55%

image.png (31.4 KB)

Why is it different from the specified value?

Why do we set different values ​​for HeightScale and WidthScale for picA?

Will the value set for IsLockAspectRatio be ignored on Aspose.Cells?

Sorry for asking so many questions. thank you.

@ymd
Through testing with sample code, we can reproduce the issue. The width and height scale settings do not match the Excel display. Please refer to the attachment. out_net.zip (8.7 KB)

As for setting different values, I think it may be due to input errors. When you set IsLockAspectRatio to true, the “Lock aspect ratio” checkbox will be selected in Excel. When you change the width or height ratio, the other one will also change accordingly.
The sample code as follows:

// Create Excel WorkBook
Console.WriteLine("Creating Excel WorkBook.");

Workbook wb = new Workbook();

Worksheet sheet = wb.Worksheets[0];
sheet.Name = "Picture";

// AllRows 10pt
sheet.Cells.StandardHeight = 20;
sheet.Cells.StandardWidth = 5;

// Discription
sheet.Cells["A1"].Value = "Test of Picture";

{
    // Marge + Line Cells (B2:I10)
    Aspose.Cells.Range rangeA = sheet.Cells.CreateRange("B2:I10");
    rangeA.Merge();
    rangeA.SetOutlineBorder(BorderType.TopBorder, CellBorderType.Thick, Color.Blue);
    rangeA.SetOutlineBorder(BorderType.BottomBorder, CellBorderType.Thick, Color.Blue);
    rangeA.SetOutlineBorder(BorderType.LeftBorder, CellBorderType.Thick, Color.Blue);
    rangeA.SetOutlineBorder(BorderType.RightBorder, CellBorderType.Thick, Color.Blue);
    Console.WriteLine($" B2:I10 Cells Points  Height:{rangeA.Height} Width:{rangeA.Width}");

    // LoadImage to B2:I10
    Picture picA = sheet.Pictures[sheet.Pictures.Add(rangeA.FirstRow, rangeA.FirstColumn, filePath + "image-600x200.png")];

    // resize to B2:I10
    picA.RelativeToOriginalPictureSize = true; // Original base Relation
    picA.IsLockAspectRatio = true; // Fix Ratio
    picA.Placement = PlacementType.FreeFloating; // Firee Floating 

    // Resize to Range Height
    picA.WidthPt = rangeA.Width;

    //add scale for width and height
    picA.HeightScale = 53;
    picA.WidthScale = 53;
}

{
    // Marge + Line Cells (B14:I10)
    Aspose.Cells.Range rangeB = sheet.Cells.CreateRange("B14:I22");
    rangeB.Merge();
    rangeB.SetOutlineBorder(BorderType.TopBorder, CellBorderType.Thick, Color.Blue);
    rangeB.SetOutlineBorder(BorderType.BottomBorder, CellBorderType.Thick, Color.Blue);
    rangeB.SetOutlineBorder(BorderType.LeftBorder, CellBorderType.Thick, Color.Blue);
    rangeB.SetOutlineBorder(BorderType.RightBorder, CellBorderType.Thick, Color.Blue);
    Console.WriteLine($" B14:I22 Cells Points  Height:{rangeB.Height} Width:{rangeB.Width}");

    // LoadImage to B14:I22
    Picture picB = sheet.Pictures[sheet.Pictures.Add(rangeB.FirstRow, rangeB.FirstColumn, filePath + "image-200x600.png")];

    // resize to B14:I22
    picB.RelativeToOriginalPictureSize = true; // Original base Relation
    picB.IsLockAspectRatio = true; // Fix Ratio
    picB.Placement = PlacementType.FreeFloating; // Firee Floating 

    // Resize to Range Height
    picB.HeightPt = rangeB.Height;

    //add scale for width and height
    picB.HeightScale = 40;
    picB.WidthScale = 40;
}
// Save the Excel file.
wb.Save(filePath + "out_net.xlsx");

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): CELLSNET-55214

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Looking at the current behavior, it appears that changing the HeightPt is not changing the Width~ value correctly according to the ratio.

What are the conditions under which “IsLockAspectRatio = true” would work correctly?

For example, if I set it based on Height~, what property should I change to automatically calculate and change Width~?

  • Height
  • HeightCM
  • HeightInch
  • HeightPt
  • HeightScale

@ymd
You can use the following sample code for testing, where only the height ratio is set, and the width ratio will also change synchronously. Please refer to the attachment. out_net.zip (8.4 KB)

// Create Excel WorkBook
Console.WriteLine("Creating Excel WorkBook.");

Workbook wb = new Workbook();

Worksheet sheet = wb.Worksheets[0];
sheet.Name = "Picture";

// AllRows 10pt
sheet.Cells.StandardHeight = 20;
sheet.Cells.StandardWidth = 5;

// Discription
sheet.Cells["A1"].Value = "Test of Picture";

{
    // Marge + Line Cells (B2:I10)
    Aspose.Cells.Range rangeA = sheet.Cells.CreateRange("B2:I10");
    rangeA.Merge();
    rangeA.SetOutlineBorder(BorderType.TopBorder, CellBorderType.Thick, Color.Blue);
    rangeA.SetOutlineBorder(BorderType.BottomBorder, CellBorderType.Thick, Color.Blue);
    rangeA.SetOutlineBorder(BorderType.LeftBorder, CellBorderType.Thick, Color.Blue);
    rangeA.SetOutlineBorder(BorderType.RightBorder, CellBorderType.Thick, Color.Blue);
    Console.WriteLine($" B2:I10 Cells Points  Height:{rangeA.Height} Width:{rangeA.Width}");

    // LoadImage to B2:I10
    Picture picA = sheet.Pictures[sheet.Pictures.Add(rangeA.FirstRow, rangeA.FirstColumn, filePath + "image-600x200.png")];

    // resize to B2:I10
    picA.RelativeToOriginalPictureSize = true; // Original base Relation
    picA.IsLockAspectRatio = true; // Fix Ratio
    picA.Placement = PlacementType.FreeFloating; // Firee Floating 

    // Resize to Range Height
    picA.WidthPt = rangeA.Width;

    //add scale for width and height
    picA.HeightScale = 53;
}


// Save the Excel file.
wb.Save(filePath + "out_net.xlsx");

Sorry for the lack of explanation.
I wanted to know if changing only which properties would work correctly.

Example:
HeightScale = value;
→ Width value does not change to a value according to scale.

HeightPt = value;
→ Width value does not change to a value according to scale.

Do I need to calculate and set both Scale and width/height for each?

If RelativeToOriginalPictureSize = true and IsLockAspectRatio = true, I would like to see that when Width~ is changed, Height~ is also changed in the same ratio as the original.
(I want it to be changed in the same way if the height and width are swapped.)

@ymd
By setting the ratio of width and height, the value of width and height can be changed simultaneously. When the proportions of 65 and 32 were set separately, the width and height values of the final file also changed accordingly. Please refer to the attachment. result.zip (16.7 KB)

@ymd
Through further testing, we can reproduce the issue. The ratios of width and height do not change simultaneously when Picture.IsLockAspectRatio is set to true. Please refer to the attachment. result.zip (47.3 KB)

The sample code as follows:

// Create Excel WorkBook
Console.WriteLine("Creating Excel WorkBook.");

Workbook wb = new Workbook();

Worksheet sheet = wb.Worksheets[0];
sheet.Name = "Picture";

// AllRows 10pt
sheet.Cells.StandardHeight = 20;
sheet.Cells.StandardWidth = 5;

// Discription
sheet.Cells["A1"].Value = "Test of Picture";

{
    // Marge + Line Cells (B2:I10)
    Aspose.Cells.Range rangeA = sheet.Cells.CreateRange("B2:I10");
    rangeA.Merge();
    rangeA.SetOutlineBorder(BorderType.TopBorder, CellBorderType.Thick, Color.Blue);
    rangeA.SetOutlineBorder(BorderType.BottomBorder, CellBorderType.Thick, Color.Blue);
    rangeA.SetOutlineBorder(BorderType.LeftBorder, CellBorderType.Thick, Color.Blue);
    rangeA.SetOutlineBorder(BorderType.RightBorder, CellBorderType.Thick, Color.Blue);
    Console.WriteLine($" B2:I10 Cells Points  Height:{rangeA.Height} Width:{rangeA.Width}");

    // LoadImage to B2:I10
    Picture picA = sheet.Pictures[sheet.Pictures.Add(rangeA.FirstRow, rangeA.FirstColumn, filePath + "image-600x200.png")];

    // resize to B2:I10
    picA.RelativeToOriginalPictureSize = true; // Original base Relation
    picA.IsLockAspectRatio = true; // Fix Ratio
    picA.Placement = PlacementType.FreeFloating; // Firee Floating 

    // Resize to Range Height
    picA.WidthPt = rangeA.Width;

    //only add scale for width
    picA.WidthScale = 32;
}


// Save the Excel file.
wb.Save(filePath + "out_32.xlsx");

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): CELLSNET-55215

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

I was able to confirm that setting both Scale properties will result in the intended resize.Thanks.

I have output with both Scale set, but the size of the photo is different from when created in Excel as shown below, will this be corrected as well?
image.png (28.9 KB)

@ymd
After setting the ratio, the displayed ratio data in Excel will be one percent larger. For example, when I set a ratio of 53, it is 54% in Excel. If you manually insert images and set the scale to 54%, you will find that two images have the same size. Please refer to the attachment. out_53.zip (8.3 KB)
After the issue CELLSNET-55214 is resolved, the issue you mentioned will be resolved simultaneously.

I understand that it deviates from the setpoint by 1%.
I also understand that this will be corrected in issue CELLSNET-55214.

Thank you for your quick response.
I hope this will be corrected soon.

@ymd
Thank you for your feedback. Once there is an update, we will notify you immediately

@ymd
Hi,
Your issue(CELLSNET-55215) has been resolved and the fixed functionality will be released with version 24.3.

1 Like

Thank you for your quick response.

Please continue with issue CELLSNET-55214 as well.

@ymd
Sure, once we resolve it, we will notify you immediately.

1 Like

The issues you have found earlier (filed as CELLSNET-55215) have been fixed in this update. This message was posted using Bugs notification tool by johnson.shi