Use following function to reduce image size. It will regenerate image with defined height and width.
public static void ResizeImage(string image, string Okey, string key, int width, int height, string newimagename)
{
System.Drawing.Image oImg = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings[Okey] + image));
System.Drawing.Image oThumbNail = new System.Drawing.Bitmap(width, height);
System.Drawing.Graphics oGraphic = System.Drawing.Graphics.FromImage(oThumbNail);
oGraphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
oGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
oGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
oGraphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
System.Drawing.Rectangle oRectangle = new System.Drawing.Rectangle(0, 0, width, height);
oGraphic.DrawImage(oImg, oRectangle);
if (newimagename == "")
{
if (image.Substring(image.LastIndexOf(".")) != ".png")
oThumbNail.Save(HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings[Okey] + image), System.Drawing.Imaging.ImageFormat.Jpeg);
else
oThumbNail.Save(HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings[Okey] + image), System.Drawing.Imaging.ImageFormat.Png);
}
else
{
if (newimagename.Substring(newimagename.LastIndexOf(".")) != ".png")
oThumbNail.Save(HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings[Okey] + newimagename), System.Drawing.Imaging.ImageFormat.Jpeg);
else
oThumbNail.Save(HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings[Okey] + newimagename), System.Drawing.Imaging.ImageFormat.Png);
}
oImg.Dispose();
}