Last post Oct 04, 2011 10:54 AM by KathyW
Oct 04, 2011 10:36 AM|vmhatup|LINK
It's an easy question, but right now my mind is exhausted, so I'm asking for help.
I have a method that resizes images (to a reduced size). It generally works, but the problem I'm having is that it resizes to a bigger size when the actual image is smaller than the thumbnail.
Here's the code that does the resizing. I would like to check that the actual image is actually bigger than the thumbnail it will create.
In the attached code, the image is resized to 300px. If the image is bigger, then it works. Unfortunately, if the image is smaller (ie. 100x100px), then the image will be resized 300x300px. In this case, the thumbnail should
Any help will be greatly appreciated.
System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(fileNameAndPath);
int newHeight = 0;
int newWidth = 0;
int iMaxSize = 300;
float aspect = (float)FullsizeImage.Height / (float)FullsizeImage.Width;
if (FullsizeImage.Height > FullsizeImage.Width)
newHeight = iMaxSize; //the new height of a 100x100px image will be 300px, which is incorrect since the image is smaller.
newWidth = (int)(iMaxSize / aspect); //the new width of a 100x100px image will be 300px, which is also incorrect since the image is smaller
newWidth = iMaxSize; //also wrong for above reasons
newHeight = (int)(iMaxSize * aspect); //also wrong for above reasons
Oct 04, 2011 10:54 AM|KathyW|LINK
Just test the original size first to see if you need to resize.
if (FullsizeImage.Height > 300 || FullsizeImage.Width > 300 ) // one or both sides greater than 300
// Your resizing code here
else // both sides are already equal to or less than 300, so keep the original sizes
newHeight = FullsizeImage.Height;
newWidth = FullsizeImage.Width;