Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload and resize image in Aspx

Tags:

c#

asp.net

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.

like image 436
Itay.B Avatar asked Nov 28 '22 03:11

Itay.B


1 Answers

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

like image 69
Fraz Sundal Avatar answered Dec 22 '22 19:12

Fraz Sundal