1
int newWidth = 100;
int newHeight = 100;
double ratio = 0;

if (img1.Width > img1.Height)
{
    ratio = img1.Width / img1.Height;
    newHeight = (int)(newHeight / ratio);
}
else
{
    ratio = img1.Height / img1.Width;
    newWidth = (int)(newWidth / ratio);
}

Image bmp1 = img1.GetThumbnailImage(newWidth, newHeight, null, IntPtr.Zero);
bmp1.Save(Server.MapPath("~/Uploads/Photos/Thumbnails/") + photo.PhotoID + ".jpg");

I always get Image with both height and width having same values (100)
I am obiously doing something wrong with type conversion?

4
  • What values do img1.Width and img1.Height have? Mar 26, 2010 at 15:27
  • @Präriewolf: I don't get any errors. @Péter Török: Hmm... I'm not pretty sure but I think it's integer because I can declare newWidth = img1.Width with no errors. Mar 26, 2010 at 15:30
  • Can you give some examples of image sizes that your are testing?
    – galford13x
    Mar 26, 2010 at 15:33
  • width=800 and height=533 Mar 26, 2010 at 15:39

3 Answers 3

12
ratio = img1.Width / img1.Height;

Width and Height are integers. You will be performing integer math on these values before storing them in your double. In integer math, 150 / 100 is 1. 199 / 100 is 1. 101 / 100 is 1. There are no decimals. After the value has been calculated, then it will be stored in your double.

Cast at least one side to double before doing your calculation.

ratio = img1.Width / (double)img1.Height;
5
  • Hmmm... now I tried to upload new photo with dimensions width=800 and height=533 and thumbnail is newWidth=800 and newHeight=66 Mar 26, 2010 at 15:35
  • @ile, I don't see where that would happen in your code snippet. Are you setting newWidth = img.Width anywhere that's not visible above? Because you shouldn't. newWidth, from your snippet, should remain 100. Mar 26, 2010 at 15:40
  • upvoted this is actually covered in one of the ms press books I went through for my foundation exam, definately need to cast one of the figures (I usually do the divisor) to double to avoid loss of precision. Mar 26, 2010 at 15:40
  • Can you step through the code with the debugger and see when the newWidth changes to 800?
    – galford13x
    Mar 26, 2010 at 15:45
  • Anthony, you were right! I left newWidth = img.Width below if statement and forgot to remove it. Thanks! Mar 26, 2010 at 15:46
1

You can say:

ratio = img1.Width / (img1.Height * 1.0);

To ensure that the value of the result is not truncated due to integer arithmetic.

1
  • 1
    Personally, I think casting (ala @Anthony's answer) makes the intent more clear.
    – JeffH
    Mar 26, 2010 at 15:34
0

What is the size of the images? if the width and height are always equal then this would make sense.

newWidth = (int)(newWidth / ratio);  // this is newWidth = newWidth / 1 so it doesn't change.
1
  • height and width are not the same. I test both horizontal and vertical photos Mar 26, 2010 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.