I want to scale images, but I don't want the image to look skewed.
The image has to be 115x115 (length x width).
The image can't be over 115 pixels high (length), but if needed, the width can be less than 115 but not more.
Is this tricky?
I want to scale images, but I don't want the image to look skewed.
The image has to be 115x115 (length x width).
The image can't be over 115 pixels high (length), but if needed, the width can be less than 115 but not more.
Is this tricky?
You're looking to scale an image and preserve Aspect Ratio:
float MaxRatio = MaxWidth / (float) MaxHeight;
float ImgRatio = source.Width / (float) source.Height;
if (source.Width > MaxWidth)
return new Bitmap(source, new Size(MaxWidth, (int) Math.Round(MaxWidth /
ImgRatio, 0)));
if (source.Height > MaxHeight)
return new Bitmap(source, new Size((int) Math.Round(MaxWidth * ImgRatio,
0), MaxHeight));
return source;
Should help you, and if you're interested in the idea:
Wikpedia article on Image Aspect Ratio http://bit.ly/d4klf
Take a look at Bertrands blog post about scaling images using GDI and WPF.
You need to preserve aspect ratio:
float scale = 0.0;
if (newWidth > maxWidth || newHeight > maxHeight)
{
if (maxWidth/newWidth < maxHeight/newHeight)
{
scale = maxWidth/newWidth;
}
else
{
scale = maxHeight/newHeight;
}
newWidth = newWidth*scale;
newHeight = newHeight*scale;
}
In the code, Initially newWidth/newHeight are width/Height of image.