We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Scale an image to fit into an Excel sheet cell in .NET

Hi,

I'm trying to display an image on excel sheet using worksheet.pictures.add(

3, 1, 35,14, "C:\\map_sample_square.JPG");

method. I have to scale every image and then display. if the image is very big or very small it should should scale according to the aspect ratio of the columns and rows specified. for eg if the image is a square, it should remain as square and should fit into the specified rows and columns. Please tell me how to do scaling of the image in excel

Thanks.

Hi,

Thanks for considering Aspose.

Well, I think you may place your image(s) into single cell into the worksheet with your desired row height and column width. So your image gets scalled according to your need.

E.g.,

Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Cells.SetRowHeight(3,140);
worksheet.Cells.SetColumnWidth(6,40);
//Picture will be placed into the G4 cell with the specified row height and col width.
int index = worksheet.Pictures.Add(3,6, 4, 7,"d:\\test\\school.jpg");
workbook.Save("d:\\test\\scaledimage.xls");
Alternatively, you may utilize Picture.Width (Pt,CM,Inch) and Picture.Height (Pt,CM,Inch) properties.
Thank you.

Hi, thanks for the reply, but I dont have that option , because I have to display the picture on a predefined template. Also I dont see any properties for the picture.Width and Picture.Height.

I'm attaching my template ,I should not change the row and column sizes.They should be as they appear in the template.All I need to do is display any picture of any size between columns 1 and 14 and rows 3 and 35 resp.

Thanks.

Please try:

worksheet.Pictures.Add(3, 1, "C:\\map_sample_square.JPG");

This is not going to scale th eomage . it displays image as it is . But I want the image to scale and fit into (3,1,14,35) upper left row, upper left col,lower right row, lower right col resp

Hi,

I tried the following code which works fine and scale the image accordingly, attached is the output file. I deleted the image into your template file and saved it as map_sample1.xls. I insert an image school.jpg into your desired area into the worksheet.

Does this code not working on your machine

Workbook workbook = new Workbook();
workbook.Open("d:\\test\\map_sample1.xls");
Worksheet worksheet = workbook.Worksheets[0];
int index = worksheet.Pictures.Add(3, 1, 35,14,"d:\\test\\school.jpg");
workbook.Save("d:\\test\\ouput_book.xls");

Thank you.

And by the way which version you are using?

Please try the attached version.