Insert a photo with the aspect ratio fixed

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

It’s been about 2 months now, how is the progress of our response ?

@ymd,

We are sorry, but your issue has not been resolved yet. We were unable to resolve it due to other priority tasks at hand. We will evaluate it and get back to you with new updates or a possible ETA on it in the next few days.

@ymd
Hi,
After careful investigation, we have discovered that your issue(CELLSNET-55214) may be caused by the “Zoom and layout” of local machine.
We guess that your setting is 125%, and the program defaults to 100%, so the result is inconsistent with expectations.
Although we provide the CellsHelper.DPI interface so that the results you generate are correct on your local machine, the results displayed on other machines may be incorrect(for 125% CellsHelper.DPI=120).
Therefore, to ensure consistent results, we strongly recommend that you change this setting to 100% and be sure to view the result files on other computers using the same settings.

Hope it helps you.
If you have further questions, please contact us.

We checked the screen settings by preparing an environment where the screen settings can be set to 100%, and confirmed that the Scale Height and Scale Width are the same value.
We recognized that the problem of the misaligned Scale values is on the Excel side.
Thank you for your detailed investigation.

@ymd
You are welcome. Thank you for your feedback. If you have any questions, please feel free to contact us.