We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Sizing pictures in XlsIO Excel spreadsheet

Hi there,

I am trying to insert an image into a worksheet and scale it so that the width of the image is the same as that of the cell.

var range = ws.Range[5, 2];
var pic = ws.Pictures.AddPicture(range.Row, range.Column, @"D:\Pictures\clouds.JPG");
int scale = (int)(range.ColumnWidth / pic.Width);
pic.Scale(scale, scale);

This results in a scale factor of 0, because as far as I can tell, the scale factors have to be integers and in percents. The use of integers for dimensions throughout the shapes functionality seemed a bit weird, but having integer scale factors is just bizarre.


Just to test it, I tried setting the picture width to the column width.

pic.Width = (int)range.ColumnWidth;
This is completely different when I open up the test file... See the attached image.

I can't say I have been very happy with the functionality for shapes in Excel so far, but if you could help it would be much appreciated :)

Attachment: excel_c7780a1.zip

2 Replies

JS Jonny Shipton March 23, 2015 04:20 PM UTC

I have managed to figure out the second issue with some help. The column width property is not in pixels, and can be converted to pixels with the IWorksheet.ColumnWidthToPixels function. :)


DB Dilli Babu Nandha Gopal Syncfusion Team March 24, 2015 05:00 PM UTC

Hi Jonny,

Thank you for using Syncfusion products.

Regarding Scale() query:

The Scale method of the IPictureShape is used to resize the image in scale width and scale height which is in percent. The below code snippet describes the implementation inside the Scale().

Code Snippet:

Width = ( int )( Width * scaleWidth / 100.0 );

Height = ( int )( Height * scaleHeight / 100.0 );

Here, the scaleWidth and scaleHeight should be in percent.

Regarding ColumnWidth/RowHeight query:

As you mentioned, for all the shape related dimensions, you must use the pixel value. The table describes the point and pixel values of the range for RowHeight and ColumnWidth.

Property in Points

Equivalent pixel values

range.ColumnWidth

worksheet.GetColumnWidthInPixels(range.Column)

range.RowHeight

worksheet.GetRowHeightInPixels(range.Row)

Please let us know if you have any concerns.

Regards,

Dilli babu.


Loader.
Live Chat Icon For mobile
Up arrow icon