Image place in cell using aspose cell c#

I am copying a image into a destination cell. It is working fine. But I am unable to perform the “Place in cell” operation as it is hovering over the cell. But I want it to be placed inside the cell.
Thank you in advance

@AbirN,

Probably, you are referring to a new feature which I think is only available in the web version and has not been made available to desktop MS Excel (e.g., 2019 or later versions), right? The feature might be in beta still. Anyways, could you please share a sample Excel file and point out the worksheet cell where you want to place the image (fitted into the cell)? Also, attach the image you want to insert, and we will assist you on how to do it via Aspose.Cells APIs.

PS. please zip the Excel file and image prior attaching here.

Exactly I want that service demonstrated in the video. In the input file you can see there is a image already placed in cell F1. I have already copied it to destination cell (B34). But I want the image to be placed inside the cell as showed in the video. In my case I’m using Microsoft 365 & it (place in cell feature) is available there.
Downloads.zip (340.6 KB)

@AbirN

The function (what the “Place in Cell” menu does in ms excel) has not been supported yet. It has been in our plan but cannot be implemented soon because of its complexity.

As an workaround, we think currently you may get the similar result(for the position and size of the copied picture) with below code:

                    Picture p = ...; //the picture at F1 for your case
                    Cell cellTo = cells[33, 1];
                    Shape shape = sheet.Shapes.AddCopy(p, cellTo.Row, 0, cellTo.Column, 0);
                    int w = cells.GetColumnWidthPixel(cellTo.Column);
                    int h = cells.GetRowHeightPixel(cellTo.Row);
                    Range r = cellTo.GetMergedRange();
                    if (r != null)
                    {
                        for (int c = r.ColumnCount - 1; c > 0; c--)
                        {
                            w += cells.GetColumnWidthPixel(r.FirstColumn + c);
                        }
                        for (int c = r.RowCount - 1; c > 0; c--)
                        {
                            h += cells.GetRowHeightPixel(r.FirstRow + c);
                        }
                    }
                    if (p.Width / (double)p.Height < w / (double)h)
                    {
                        shape.Height = h;
                        int pw = (int)(p.Width / (double)p.Height * h);
                        shape.Width = pw;
                        int l = (w - pw) >> 1;
                        if (r != null && r.ColumnCount > 1)
                        {
                            w = cells.GetColumnWidthPixel(r.FirstColumn);
                            if (l > w)
                            {
                                for (int c = 1; c < r.ColumnCount; c++)
                                {
                                    l -= w;
                                    w = cells.GetColumnWidthPixel(r.FirstColumn + c);
                                    if (l < w)
                                    {
                                        shape.UpperLeftColumn = r.FirstColumn + c;
                                        shape.Left = l;
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                shape.Left = l;
                            }
                        }
                    }
                    else
                    {
                        shape.Width = w;
                        int ph = (int)(p.Height / (double)p.Width * w);
                        shape.Height = ph;
                        int t = (h - ph) >> 1;
                        if (r != null && r.RowCount > 1)
                        {
                            h = cells.GetRowHeightPixel(r.FirstRow);
                            if (t > h)
                            {
                                for (int c = 1; c < r.RowCount; c++)
                                {
                                    t -= h;
                                    h = cells.GetRowHeightPixel(r.FirstRow + c);
                                    if (t < h)
                                    {
                                        shape.UpperLeftRow = r.FirstRow + c;
                                        shape.Top = t;
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                shape.Top = t;
                            }
                        }
                    }

And if it can be more convenient for users to support such kind of implementation by our component, we may consider to provide one new api for it later. Thank you.

@AbirN
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.

@AbirN
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");
1 Like