Free Support Forum - aspose.com

Problem with image resize

Hi. I’m getting some strange behavior from Aspose.Excel.Pictures.Add(int, int, Stream, int, int);

The image in the stream has dimensions of 1141x700. I have to constrain it to fit in a space that’s 92x58 (according to the pixel count shown in Excel when I click on the divider between cells).

I get the two percentages with are around 5% and around 8%.

Because I want to constrain the image, but maintain image dimensions I use the smaller of the two percentages (5%) for both width and height.

However, in my excel document the resulting image is WAY too small, about 5x5 pixels (though there doesn’t seem to be a way to get the exact pixel count in excel).

Maybe I’m just making a simple arithmetic mistake, but I don’t think so. Please take a look at the code below and tell me what I’m doing wrong. Thanks!

A code fragment:

//get image dimensions
int imageHeight = oImage.Height;
int imageWidth = oImage.Width;

//compute percentage by which to constrain width and height
double heightPercentage = ((double)iMaxHeight) / ((double)imageHeight) * 100;
double widthPercentage = ((double)iMaxWidth) / ((double)imageWidth) * 100;

//to maintain the dimensions of the photo, we take whichever percentage is smallest and
//use that for both the width and height constraints
if (heightPercentage > widthPercentage) heightPercentage = widthPercentage;
else if (widthPercentage > heightPercentage) widthPercentage = heightPercentage;

// Use aspose to put the image
Aspose.Excel.Pictures oPics = pWorksheet.Pictures;
// Create a range based on sCell which we got above
Aspose.Excel.Range oRange = pWorksheet.Cells.CreateRange(sCell,sCell);

int iPicIndex = oPics.Add( oRange.FirstRow, oRange.FirstColumn, oStream, (int) widthPercentage, (int) heightPercentage );

Which version of Aspose.Excel are you using?

Please try this piece of code:

int iPicIndex = oPics.Add( oRange.FirstRow, oRange.FirstColumn, oStream, 5, 5 );

We’re using Aspose.Excell.dll version 3.6.0, runtime version (not sure what that is) v1.0.3705.

I tried your piece of code and, sure enough, it displayed a very tiny image. I am unable to verify, however, whether the image it displays is 5% or the original or not. The last two arguments (5, 5) are percentages, right?

The issue is not that I can’t get the function to compile/run, but that the images appear much smaller than I believe that I am telling them to be. I am computing a reasonable percentage (or so i think, anyway… see code fragment above) but the images still appear much smaller than anticipated. Am I doing something stupid?

Thanks.


Please right click those images in your Excel files to see if they are in correct scale percentage.

Right click the image -> Format Pictures ->Size.

If you still cannot figure it out. Please post the image file here and please post the following Excel files:

1. Excel file created with your code

2. Excel file manually created with your expected pictures.

I will check them.

No, you’re right. It’s working correctly. must be something wrong with my code. I’ll go back into the debugger and see what I can see.

Thanks for your help.