How to Fit Image to Cell Width in Aspose.Cells Instead of Fixed Width?

Hello,

I’m trying to fit images to the width of a cell. Currently, I’m using the following code:

C#
foreach (Shape s in doc.GetChildNodes(NodeType.Shape, true))
{
s.AspectRatioLocked = true;
s.Width = 100; // This sets a fixed width
}

However, this approach sets the width to 100 points, which is not dynamic. I need the image to automatically adjust to the width of the cell it’s placed in, while maintaining the aspect ratio.

Question:
How can I programmatically fit an image to match the width of a specific cell in Aspose.Cells (instead of using a fixed width)? Is there a built-in method or do I need to calculate the cell width and set the image size accordingly?

Thank you!

@e.scanlan

To fit an image to the width of a specific cell in Aspose.Cells while maintaining the aspect ratio, you need to calculate the cell’s width and then set the image’s width accordingly. Here’s how you can achieve this:

  1. Retrieve the width of the target cell.
  2. Set the image’s width to match the cell’s width.
  3. Adjust the height to maintain the aspect ratio.

Here’s a code snippet to demonstrate this:

// Assuming 'doc' is your Workbook and 'sheet' is the target Worksheet
var cell = sheet.Cells["A1"]; // Specify the cell you want to fit the image to
double cellWidth = cell.GetWidth(); // Get the width of the cell

foreach (Shape shape in doc.GetChildNodes(NodeType.Shape, true))
{
    shape.AspectRatioLocked = true; // Maintain aspect ratio
    shape.Width = cellWidth; // Set the width to the cell's width

    // Adjust the height to maintain aspect ratio
    double aspectRatio = shape.Height / shape.Width;
    shape.Height = cellWidth * aspectRatio; // Adjust height based on aspect ratio
}

This code ensures that the image fits the width of the specified cell while preserving its original aspect ratio. Adjust the cell reference as needed for your specific use case.

If you have any further questions or need additional assistance, feel free to ask!

@e.scanlan
There are solutions to insert image
1, Place in cell:

The picture will auto fit the size of the cell.
2, Insert Picture over Cells Using Excel

Please use cells.GetColumnWidthPixel() and cells.GetRowHeightPixel() method to get row height and column width, then set width and height of picture.

@e.scanlan,

Moreover, if you don’t want to use Place in Cell feature, you can add the picture with customized size (which will be done automatically), please see the following example (I have also added a few comments for your reference):

const string imagePath = "e:\\test2\\Aspose\\logo.png";
const string outputFilePath = "e:\\test2\\outTest1.xlsx";
Workbook workbook = new Workbook();
WorksheetCollection worksheets = workbook.Worksheets;
Worksheet worksheet = worksheets.Add("My Worksheet");
worksheet.Cells["C2"].Value = "Image";
worksheet.Cells.SetRowHeight(3, 150);//Extend 4th row height 150 points 
worksheet.Cells.SetColumnWidth(2, 150);//Set C column width 150 chars
//Insert picture to fit into C4 cell - it will automatically customize the size of the image since we set value 4 (for lowerRightRow (third parameter)) and value 3 (for lowerRightColumn (4th parameter))  for the overload/method.
int index = worksheet.Pictures.Add(3, 2, 4, 3, imagePath);
Aspose.Cells.Drawing.Picture pic = worksheet.Pictures[index];
pic.Placement = PlacementType.FreeFloating;
workbook.Save(outputFilePath);

Simply put, you need to increment the first and second parameters, upperLeftRow and upperLeftColumn, by +1 to determine the third and fourth parameters accordingly. This will ensure the image to be fit into the cell.

Hope, this helps a bit.