I want to resize big images to have a width of 150 pixels and the height will be fixed. and then save the image into a folder in my website.
examples: (width,height) if I resize an image 300px*300px I will get a resized image of 150px*150px. if I resize an image 450px*300px I will get a resized image of 150px*100px.
the point is that the ratio between width and height will be saved always keeping a width of 150 pixels.
Any Help?
I found this (not mine) here
private Bitmap ScaleImage(Image oldImage)
{
double resizeFactor = 1;
if (oldImage.Width > 150 || oldImage.Height > 150)
{
double widthFactor = Convert.ToDouble(oldImage.Width) / 150;
double heightFactor = Convert.ToDouble(oldImage.Height) / 150;
resizeFactor = Math.Max(widthFactor, heightFactor);
}
int width = Convert.ToInt32(oldImage.Width / resizeFactor);
int height = Convert.ToInt32(oldImage.Height / resizeFactor);
Bitmap newImage = new Bitmap(width, height);
Graphics g = Graphics.FromImage(newImage);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(oldImage, 0, 0, newImage.Width, newImage.Height);
return newImage;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With