I have a page which allows the user to upload pictures. The problem is that my old users can't resize the image themselves. I want to allow them to upload any size of image and then when the server gets it, it will create a small copy of this picture.
There are so many approaches to resize images, but i like this one
System.Drawing.Image image = System.Drawing.Image.FromFile("FilePath");
int newwidthimg = 160;
float AspectRatio = (float)image.Size.Width / (float)image.Size.Height;
int newHeight = Convert.ToInt32(newwidthimg / AspectRatio);
Bitmap thumbnailBitmap = new Bitmap(newwidthimg, newHeight);
Graphics thumbnailGraph = Graphics.FromImage(thumbnailBitmap);
thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
var imageRectangle = new Rectangle(0, 0, newwidthimg, newHeight);
thumbnailGraph.DrawImage(image, imageRectangle);
thumbnailBitmap.Save("FilePath", ImageFormat.Jpeg);
thumbnailGraph.Dispose();
thumbnailBitmap.Dispose();
image.Dispose();
I have fix the width because i want all my images having width 160 and height according to the Aspect ratio
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