Reduce image file size without resize in ASP.Net MVC

Last Reply 10 days ago By Lipi

Posted 18 days ago

I am able to convert the image format to jpeg format using the below code.

But Image size is not getting reduce as after converting image from png to jpeg format. 

Please suggest, if anybody has any solution. Thanks in advance.

        [HttpGet]
		public ActionResult UploadFile()
		{
			return View();
		}
		[HttpPost]
		public ActionResult UploadFile(HttpPostedFileBase file)
		{
			try
			{
				if (Request.Files.Count > 0)
				{
					HttpPostedFileBase File = Request.Files[0];
					if (File != null && File.ContentLength > 0)
					{
						if (Path.GetExtension(file.FileName) != "jpeg")
						{
							string trailingPath = Path.GetFileName(file.FileName);
							string fullPath = Server.MapPath("~/images/");
							file.SaveAs(fullPath+file.FileName.Replace("png","jpeg"));
                                                }
                                            }
                                        }
                                    }

 

@{
	ViewBag.Title = "UploadFile";
}

<h2>UploadFile</h2>

@using (Html.BeginForm("UploadFile", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div>
	<input id="File" name="File" type="file" style="width:100%" accept="image/*" />
	<input type="submit" value="Upload" />
	@ViewBag.Message
    </div>
} 

 

You are viewing reply posted by: Lipi 10 days ago.
Posted 10 days ago

Hi All,

i am able to reduce the size of the image without specifying the heighta nd width of the image specifically and able to convert to different image format as well.

please find the below code. This is the complete solution. 

                [HttpPost]
		public ActionResult UploadFile(HttpPostedFileBase file)
		{
			if (file != null && file.ContentLength > 0)
			{
				string extension = Path.GetExtension(file.FileName);
				
				if (extension != ".jpeg" || extension != ".jpg")
				{
					string fullPath = Server.MapPath("~/Images/");
					file.SaveAs(fullPath + file.FileName);
					System.Drawing.Image image = System.Drawing.Image.FromFile(fullPath + file.FileName);
					var jpgEncoder = GetEncoder(ImageFormat.Jpeg);
					var qualityEncoder = Encoder.Quality;
					var encoderParameters = new EncoderParameters(1);
					encoderParameters.Param[0] = new EncoderParameter(qualityEncoder, 50L);
					image.Save(fullPath + file.FileName.Replace(extension, ".jpeg"), jpgEncoder, encoderParameters);
				}
			}

			return View();
		}
		private ImageCodecInfo GetEncoder(ImageFormat format)
		{
			var codecs = ImageCodecInfo.GetImageDecoders();
			foreach (var codec in codecs)
			{
				if (codec.FormatID == format.Guid)
				{
					return codec;
				}
			}

			return null;
		}

 

@{
	ViewBag.Title = "UploadFile";
}

<h2>UploadFile</h2>

@using (Html.BeginForm("UploadFile", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div>
	<input id="File" name="File" type="file" style="width:100%" accept="image/*" />
	<input type="submit" value="Upload" />
	@ViewBag.Message
    </div>
}