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)
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:
…
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.
@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.
@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.
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