Including image to worksheet and make it fit in one row

Good afternoon,

Is there a way to include an image dynamically using Aspose Cells other than the following?

sheet.Pictures.Add(1, 1,"C:\\image.gif");

I want to specify somehow to include the image in a single row as shown in the attached "AddingImage_single_row.pngAddingImage_single_row.png". I used Pictures.Add method and I was able to make it looks like in the AddingImage.pngAddingImage.png image but my client is not happy because the image is being allocated in 4 rows instead of 1.

It is important to say that I don't know the dimensions of the image in advance, it has to be added without any manual intervention.

Make sense?

Regards,

Annie.

Hi,

Thanks for your posting and using Aspose.Cells for .NET.

Please download and use the latest version:
Aspose.Cells
for .NET v7.3.1.4



You should add the image and then adjust the row’s and column’s height accordingly.

Please see the code below it inserts a picture inside some cell and adjust the cell’s height and width accordingly.

I have attached the source image, output file and the screenshot for your reference. You can modify this code according to your needs.

C#


public static void AddPicture(Worksheet Sheet, int Row, int Column, int Width, int Height, string PicPath)

{

//Set the height of the first row

Sheet.Cells.SetRowHeightPixel(Row, Height); //height equals to picture height


//Set the width of the first and second column

Sheet.Cells.SetColumnWidthPixel(Column, Width);


//Add a picture inside cell at this row and column

int picId = Sheet.Pictures.Add(Row, Column, PicPath);

Picture pic = Sheet.Pictures[picId];


//Set the height and width of picture

pic.Height = Height;

pic.Width = Width;

}


public static void Run()

{

//Create a workbook

Workbook workbook = new Workbook();


//Access first sheet

Worksheet worksheet = workbook.Worksheets[0];


//Add a picture inside a cell

AddPicture(worksheet, 4, 4, 300, 200, @“f:\downloads\image-koala.jpg”);


//Write the output

workbook.Save(@“f:\downloads\output.xlsx”, SaveFormat.Xlsx);

}


Screenshot & Source Image: