Resizing Image in C# (Without Losing Quality )

You can resize the image before saving it , if you are using the fileupload then this code will help you to resize the image with no effect on the picture quality and you can save it in any format such as .png ,.jpeg, .bmp and .gif etc.

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Drawing.Imaging;

 

fileupload1.SaveAs(Server.MapPath("images/tempImage/" + fileupload1.FileName));

System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath("images/tempImage/" + fileupload1.FileName));

int newwidthimg = 200;

float AspectRatio = (float)image.Size.Width / (float)image.Size.Height;

int newHeight = 200;

Bitmap bitMAP1 = new Bitmap(newwidthimg, newHeight);

Graphics imgGraph = Graphics.FromImage(bitMAP1);

bitMAP1.imgQuality = CompositingQuality.HighQuality;

bitMAP1.SmoothingMode = SmoothingMode.HighQuality;

bitMAP1.InterpolationMode = InterpolationMode.HighQualityBicubic;

var imgDimesions = new Rectangle(0, 0, newwidthimg, newHeight);

bitMAP1.DrawImage(image, imageRectangle);

bitMAP1.Save(Server.MapPath("images/Shops/" +fileupload1.filename), ImageFormat.Jpeg);

bitMAP1.Dispose();

bitMAP1.Dispose();

image.Dispose();

 

if (System.IO.File.Exists(Server.MapPath("images/tempImage/" + fileupload1.FileName)))

{

      System.IO.File.Delete(Server.MapPath("images/tempImage/" + fileupload1.FileName));

}