How to place an image in cell basically in excel there is a feature Place in cell in case of image how to do that in aspose c#

I have a excel sheet containing image now I want copy that image paste into another sheet cell and place the image in cell…how to do that?

@Soumen2001,

Could you please share a sample Excel file containing the image into a cell and point out where in worksheet cell you want to copy the image (fitted into the cell)? Also, give us your expected Excel file in which the image is well copied/place into other worksheet cell. We will check on how to do it via Aspose.Cells APIs.

PS. please zip the Excel file prior attaching.

@Soumen2001
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-55066

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.

@Soumen2001
This feature (“Place in Cell” menu in ms excel) is not supported for now. As an alternative, you can refer to the following sample code to implement the requirements. Please refer to the attachment. result.zip (127.9 KB)

The sample code as follows:

Workbook wb = new Workbook(filePath + "sample.xlsx");
int index = wb.Worksheets.Add();
Worksheet destSheet = wb.Worksheets[index];
Cells destCells = destSheet.Cells;
PictureCollection pictures = wb.Worksheets[0].Pictures;


Picture picture = pictures[0]; //the picture at the first worksheet
Cell destCell = destCells[8, 5];
Shape shape = destSheet.Shapes.AddCopy(picture, destCell.Row, 0, destCell.Column, 0);
int columnWidth = destCells.GetColumnWidthPixel(destCell.Column);
int rowHeight = destCells.GetRowHeightPixel(destCell.Row);
Range range = destCell.GetMergedRange();
if (range != null)
{
    for (int c = range.ColumnCount - 1; c > 0; c--)
    {
        columnWidth += destCells.GetColumnWidthPixel(range.FirstColumn + c);
    }
    for (int r = range.RowCount - 1; r > 0; r--)
    {
        rowHeight += destCells.GetRowHeightPixel(range.FirstRow + r);
    }
}
if (picture.Width / (double)picture.Height < columnWidth / (double)rowHeight)
{
    shape.Height = rowHeight;
    int percentWidth = (int)(picture.Width / (double)picture.Height * rowHeight);
    shape.Width = percentWidth;
    int left = (columnWidth - percentWidth) >> 1;
    if (range != null && range.ColumnCount > 1)
    {
        columnWidth = destCells.GetColumnWidthPixel(range.FirstColumn);
        if (left > columnWidth)
        {
            for (int c = 1; c < range.ColumnCount; c++)
            {
                left -= columnWidth;
                columnWidth = destCells.GetColumnWidthPixel(range.FirstColumn + c);
                if (left < columnWidth)
                {
                    shape.UpperLeftColumn = range.FirstColumn + c;
                    shape.Left = left;
                    break;
                }
            }
        }
        else
        {
            shape.Left = left;
        }
    }
}
else
{
    shape.Width = columnWidth;
    int percentHeight = (int)(picture.Height / (double)picture.Width * columnWidth);
    shape.Height = percentHeight;
    int top = (rowHeight - percentHeight) >> 1;
    if (range != null && range.RowCount > 1)
    {
        rowHeight = destCells.GetRowHeightPixel(range.FirstRow);
        if (top > rowHeight)
        {
            for (int r = 1; r < range.RowCount; r++)
            {
                top -= rowHeight;
                rowHeight = destCells.GetRowHeightPixel(range.FirstRow + r);
                if (top < rowHeight)
                {
                    shape.UpperLeftRow = range.FirstRow + r;
                    shape.Top = top;
                    break;
                }
            }
        }
        else
        {
            shape.Top = top;
        }
    }
}

wb.Save(filePath + "out_net.xlsx");

Hope helps a bit.

no this is not working…picture is covering the cell border

@Soumen2001
You can add boundary tolerance when setting the image position to fine tune the position. Please review the result file. out_net.zip (65.4 KB)

The sample code as follows:

Workbook wb = new Workbook(filePath + "sample.xlsx");
int index = wb.Worksheets.Add();
Worksheet destSheet = wb.Worksheets[index];
Cells destCells = destSheet.Cells;
PictureCollection pictures = wb.Worksheets[0].Pictures;

int toleranceEdge = 1;
Picture picture = pictures[0]; //the picture at the first worksheet
Cell destCell = destCells[8, 5];
            
Shape shape = destSheet.Shapes.AddCopy(picture, destCell.Row, toleranceEdge, destCell.Column, toleranceEdge);
int columnWidth = destCells.GetColumnWidthPixel(destCell.Column);
int rowHeight = destCells.GetRowHeightPixel(destCell.Row);
Range range = destCell.GetMergedRange();
if (range != null)
{
    for (int c = range.ColumnCount - 1; c > 0; c--)
    {
        columnWidth += destCells.GetColumnWidthPixel(range.FirstColumn + c);
    }
    for (int r = range.RowCount - 1; r > 0; r--)
    {
        rowHeight += destCells.GetRowHeightPixel(range.FirstRow + r);
    }
}
if (picture.Width / (double)picture.Height < columnWidth / (double)rowHeight)
{
    shape.Height = rowHeight - toleranceEdge * 2;
    int percentWidth = (int)(picture.Width / (double)picture.Height * rowHeight);
    shape.Width = percentWidth - toleranceEdge * 2;
    int left = (columnWidth - percentWidth) >> 1;
    if (range != null && range.ColumnCount > 1)
    {
        columnWidth = destCells.GetColumnWidthPixel(range.FirstColumn);
        if (left > columnWidth)
        {
            for (int c = 1; c < range.ColumnCount; c++)
            {
                left -= columnWidth;
                columnWidth = destCells.GetColumnWidthPixel(range.FirstColumn + c);
                if (left < columnWidth)
                {
                    shape.UpperLeftColumn = range.FirstColumn + c;
                    shape.Left = left;
                    break;
                }
            }
        }
        else
        {
            shape.Left = left;
        }
    }
}
else
{
    shape.Width = columnWidth - toleranceEdge * 2;
    int percentHeight = (int)(picture.Height / (double)picture.Width * columnWidth);
    shape.Height = percentHeight - toleranceEdge * 2;
    int top = (rowHeight - percentHeight) >> 1;
    if (range != null && range.RowCount > 1)
    {
        rowHeight = destCells.GetRowHeightPixel(range.FirstRow);
        if (top > rowHeight)
        {
            for (int r = 1; r < range.RowCount; r++)
            {
                top -= rowHeight;
                rowHeight = destCells.GetRowHeightPixel(range.FirstRow + r);
                if (top < rowHeight)
                {
                    shape.UpperLeftRow = range.FirstRow + r;
                    shape.Top = top;
                    break;
                }
            }
        }
        else
        {
            shape.Top = top;
        }
    }
}

wb.Save(filePath + "out_net.xlsx");

We will continue to investigate and analyze your issue and notify you immediately once there are any updates.

out_net.zip (92.4 KB)
I just want output like this

@Soumen2001
Thanks for further details. Let us investigate and analyze your issue in details. Once we have an update on it, we will let you know.

Is there any update?

@Soumen2001
No updates on your issue. Once we have any new information, we will share it with you. We will get back to you soon.

@Soumen2001
At present, Aspose.Cells doesn’t support placing an image in cell. This is a relatively complex feature, and we need more time to analyze and study it. We strive to support this feature in the second quarter of 2024. Sorry for any inconvenience caused to you.

@Soumen2001
Please try the latest version 24.3

  Workbook workbook = new Workbook();
            workbook.Worksheets[0].Cells["B3"].EmbeddedImage = File.ReadAllBytes("2.png");
            workbook.Save("dest.xlsx");