Compressed Image Uploading

May 12 2021 5:17 AM
Hi,
 
I want to compress my image while I'm Uploading. With the help of the C#Corner Website I got that code. But, I facing the Issue in
  1. Stream strm = ImageFile.PostedFile.InputStream; // Here It shows error in Input stream  
  2. using (var image = System.Drawing.Image.FromStream(strm))  
  3. {   
My Full code is here :
  1. public ActionResult DetailsEdit(EmployeeData___TBL e, string id, HttpPostedFileBase ImageFile)  
  2. {  
  3.     var data = Session["userid"].ToString();  
  4.     if (e.ImageFile != null)  
  5.     {  
  6.         string filename = Path.GetFileNameWithoutExtension(e.ImageFile.FileName);  
  7.         string extension = Path.GetExtension(e.ImageFile.FileName);  
  8.         HttpPostedFileBase postedfile = e.ImageFile;  
  9.         int length = postedfile.ContentLength;  
  10.         if (extension.ToLower() == ".jpg" || extension.ToLower() == ".jpeg" || extension.ToLower() == ".png")  
  11.         {  
  12.             if (length <= 1000000)  
  13.             {  
  14.                 filename = filename + extension;  
  15.                 Stream strm = ImageFile.PostedFile.InputStream;  
  16.                 using (var image = System.Drawing.Image.FromStream(strm))  
  17.                 {  
  18.                     int newWidth = 240; // New Width of Image in Pixel    
  19.                     int newHeight = 240; // New Height of Image in Pixel    
  20.                     var thumbImg = new Bitmap(newWidth, newHeight);  
  21.                     var thumbGraph = Graphics.FromImage(thumbImg);  
  22.                     thumbGraph.CompositingQuality = CompositingQuality.HighQuality;  
  23.                     thumbGraph.SmoothingMode = SmoothingMode.HighQuality;  
  24.                     thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;  
  25.                     var imgRectangle = new Rectangle(0, 0, newWidth, newHeight);  
  26.                     thumbGraph.DrawImage(image, imgRectangle);  
  27.                     e.ProfileImage = "~/AppFiles/" + filename;  
  28.                     filename = Path.Combine(Server.MapPath("~/AppFiles/"), filename);  
  29.                     e.ImageFile.SaveAs(filename);  
  30.                     // db.Entry(e).State = EntityState.Modified;  
  31.                     // int a = db.SaveChanges();  
  32.                     db.SP_EditUserInfo(data, e.Name, e.EmailID, e.PhoneNo, e.Address, e.ProfileImage);  
  33.                     //TempData["UpdateMessage"] = "<script>alert('Data Updated Successfully.')</script>";  
  34.                     ModelState.Clear();  
  35.                     return RedirectToAction("Details""Home");  
  36.                 }  
  37.             }  
  38.         }  
  39.         else  
  40.         {  
  41.             e.ProfileImage = Session["Image"].ToString();  
  42.             //db.SP_UpdateUserInfo(e.UserId, e.Name, e.EmailID, e.PhoneNo, e.Address, e.ProfileImage);  
  43.             //db.Entry(e).State = EntityState.Modified;  
  44.             ModelState.Clear();  
  45.             return RedirectToAction("List""Home");  
  46.         }  
  47.         return View();  
  48.     }  
  49. }  

Answers (7)