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>
}