Saturday, 5 September 2020

File Resizing C# | ASP.NET | MVC

 

You can resize your image as per your requirement by using WebImage Class which implemented in System.Web.Helpers namespace and resizing is done by Resize and save method.

Namespace : using System.Web.Helpers;

[HttpPost]

        public JsonResult UploadCommonImage(string fnm, string pth,string w,string h)

        {

            if (Request.Files.Count > 0)

            {               

                try

                {

                    HttpFileCollectionBase files = Request.Files;

                    for (int i = 0; i <= files.Count - 1; i++)

                    {

                        HttpPostedFileBase file = files[i];

                        string fname = files[0].FileName;

                        string fileName = fnm;

                        string fileExtension = Path.GetExtension(fname);

                        fname = fileName + fileExtension;

                        fname = Path.Combine(Server.MapPath(pth), fname);

                        WebImage img = new WebImage(file.InputStream);

                        img.Resize(Convert.ToInt16(w),Convert.ToInt16(h), false, false);

                        fname = Path.Combine(Server.MapPath(pth), fname);

                        img.Save(fname);

                    }

                    var msg = "1";

                    return Json(msg, JsonRequestBehavior.AllowGet);

                }

                catch (Exception ex)

                {

                    return Json("Error occurred. Error details: " + ex.Message);

                }

            }

            else

            {

                return Json("No files selected.");

            }

        }


Next Topic

No comments:

Post a Comment