facebooktwittermenuarrow-up

GemBox Support Forum

Add multiple images in single cell in Excel file with auto size

i wanted to add multiple images in a single cell in an excel file with auto size.
kindly let us know how to proceed.

Hi Naresh,

What do you mean by auto size?

Anyway, you can place the beginning of the image to an ExcelCell like this:

var workbook = ExcelFile.Load("input.xlsx");
var worksheet = workbook.Worksheets[0];

double columnOffset = 20;
double rowOffset = 5;

var anchor = new AnchorCell(worksheet.Columns["A"], worksheet.Rows["1"], columnOffset, rowOffset, LengthUnit.Pixel);
var picture = worksheet.Pictures.Add("image.png", anchor);

workbook.Save("output.xlsx");

The size of the ExcelPicture element will be automatically set based on the size of the source image.

I hope this helps.

Regards,
Mario

Hi Mario,

i wanted to place multiple images in column A and row 1 side by side.
right now if i add images it’s placing one above the other.
below is the code that I am using

var image1 = new Bitmap(@"C:\Users\nares\source\repos\ExportToExcel\emptycart.JPG", true);
var image2 = new Bitmap(@"C:\Users\nares\source\repos\ExportToExcel\agcli.JPG", true);

var totalHeight = image1.Height + image2.Height;
var totalWidth = image1.Width + image2.Width;

worksheet.Pictures.Add(@"C:\Users\nares\source\repos\ExportToExcel\emptycart.JPG", "A1",
    image1.Width, image1.Height, LengthUnit.Pixel);
 worksheet.Pictures.Add(@"C:\Users\nares\source\repos\ExportToExcel\agcli.JPG", "A1",
    image2.Width, image2.Height, LengthUnit.Pixel);

worksheet.Columns[0].SetWidth(totalWidth, LengthUnit.Pixel);
worksheet.Rows[0].SetHeight(totalHeight, LengthUnit.Pixel);

workbook.Save("Images.xlsx");

what I wanted is to place images side by side rather than overlapping.

Hi Naresh,

Try this:

var picture1 = worksheet.Pictures.Add(@"C:\Users\nares\source\repos\ExportToExcel\emptycart.JPG", "A1");
var picture2 = worksheet.Pictures.Add(@"C:\Users\nares\source\repos\ExportToExcel\agcli.JPG", "A1");

picture2.Position.From.SetColumnOffset(picture1.Position.Width, LengthUnit.Point);

var totalWidth = picture1.Position.Width + picture2.Position.Width;
var totalHeight = Math.Max(picture1.Position.Height, picture2.Position.Height);

worksheet.Columns[0].SetWidth(totalWidth, LengthUnit.Point);
worksheet.Rows[0].SetHeight(totalHeight, LengthUnit.Point);

Does this solve your issue?

Regards,
Mario