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
- Stream strm = ImageFile.PostedFile.InputStream; // Here It shows error in Input stream
- using (var image = System.Drawing.Image.FromStream(strm))
- {
My Full code is here :
- public ActionResult DetailsEdit(EmployeeData___TBL e, string id, HttpPostedFileBase ImageFile)
- {
- var data = Session["userid"].ToString();
- if (e.ImageFile != null)
- {
- string filename = Path.GetFileNameWithoutExtension(e.ImageFile.FileName);
- string extension = Path.GetExtension(e.ImageFile.FileName);
- HttpPostedFileBase postedfile = e.ImageFile;
- int length = postedfile.ContentLength;
- if (extension.ToLower() == ".jpg" || extension.ToLower() == ".jpeg" || extension.ToLower() == ".png")
- {
- if (length <= 1000000)
- {
- filename = filename + extension;
- Stream strm = ImageFile.PostedFile.InputStream;
- using (var image = System.Drawing.Image.FromStream(strm))
- {
- int newWidth = 240; // New Width of Image in Pixel
- int newHeight = 240; // New Height of Image in Pixel
- var thumbImg = new Bitmap(newWidth, newHeight);
- var thumbGraph = Graphics.FromImage(thumbImg);
- thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
- thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
- thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
- var imgRectangle = new Rectangle(0, 0, newWidth, newHeight);
- thumbGraph.DrawImage(image, imgRectangle);
- e.ProfileImage = "~/AppFiles/" + filename;
- filename = Path.Combine(Server.MapPath("~/AppFiles/"), filename);
- e.ImageFile.SaveAs(filename);
- // db.Entry(e).State = EntityState.Modified;
- // int a = db.SaveChanges();
- db.SP_EditUserInfo(data, e.Name, e.EmailID, e.PhoneNo, e.Address, e.ProfileImage);
- //TempData["UpdateMessage"] = "";
- ModelState.Clear();
- return RedirectToAction("Details", "Home");
- }
- }
- }
- else
- {
- e.ProfileImage = Session["Image"].ToString();
- //db.SP_UpdateUserInfo(e.UserId, e.Name, e.EmailID, e.PhoneNo, e.Address, e.ProfileImage);
- //db.Entry(e).State = EntityState.Modified;
- ModelState.Clear();
- return RedirectToAction("List", "Home");
- }
- return View();
- }
- }

Sachin SinghRank #66 · 26.2K reputationPosted May 12, 2021, 6:02 AM EDT
Sachin SinghRank #66 · 26.2K reputationPosted May 12, 2021, 4:18 AM EDT
Sachin SinghRank #66 · 26.2K reputationPosted May 12, 2021, 5:53 AM EDT
Salman BegRank #284 · 8.8K reputationPosted May 12, 2021, 2:57 AM EDT
vijayaasri palaniappan120 reputationPosted May 12, 2021, 3:20 AM EDT
vijayaasri palaniappan120 reputationPosted May 12, 2021, 4:27 AM EDT
Kip HackmanRank #2,595 · 536 reputationPosted Nov 2, 2022, 4:50 PM EDT
You can use LEADTOOLS Imaging Pro SDK technology in your application.
https://www.leadtools.com/sdk/products/imaging-pro
You can leverage the RasterCodecs class, which allows you to load any LEADTOOLS supported raster image format and export to any LEADTOOLS supported raster image format. You can utilize the RasterImageFormat enumeration and QualityFactor property, to indicate the image compression type and to specify the tradeoff between quality and compression.
https://www.leadtools.com/help/sdk/v21/dh/to/compression-quality-factors.html
I am unaware of the file format you are using, so the code below uses JPEG:
// Load in source image using (RasterCodecs _codecs = new RasterCodecs()) using (RasterImage _image = _codecs.Load(@"FILE PATH TO INPUT IMAGE")) { // QualityFactor takes values between 2 to 255 // 2 being highest quality, 255 being most compression _codecs.Options.Jpeg.Save.QualityFactor = 100; _codecs.Save(_image, @"FILE PATH TO OUTPUT IMAGE", RasterImageFormat.Jpeg, 0); }