Compress image using ImageCompress NuGet package | Code2night.com
Code2night
  • Home
  • Blogs
  • Guest Posts
  • Tutorial
  • Post Blog
  • Register
  • Login
  1. Home
  2. Blogpost

Compress image using ImageCompress NuGet package

Date- Jul 22,2022

4938

Free Download
compress images csharp compress jpeg csharp


ImageCompress

This nuget package is a free third party package which you can install in your project and then add code for compressing image in few steps. So we will see how to use this.

Step 1: Install the NuGet package in the solution

 Install-Package ImageCompress -Version 1.0.0

So, first of all you have to install this nuget package which you can do by using the command which is mentioned above or you can install the package which is shown in below image.

After, you have done installing the package you can now use the package, you can create one action as you can see in below image and that is where we will get our file which we have to compress . As our package requires the file in byte array , we have to convert the file to byte array to compress it.

         [HttpPost]
        public ActionResult Compression(HttpPostedFileBase file)
        {
            try
            {
                using (var mS = new MemoryStream())
                {
                    var targetpath = Server.MapPath("/Content/Compressed/");
                    var imageName = Path.GetFileNameWithoutExtension(file.FileName) + "_Compressed" + DateTime.Now.ToString("ddMMyyyyhhmmss") + Path.GetExtension(file.FileName);
                    byte[] image = new byte[file.ContentLength];
                    file.InputStream.Read(image, 0, image.Length);
                    CompressImage.CompressingImage(targetpath, imageName, image, 900.0f, 900.0f);
                }
            }
            catch (Exception ex)
            {
                string exc = ex.Message;
            }
            return RedirectToAction("Index");
        }

Now on the view we will add a form , where we will add one fileupload control and there we will select the image file which we will compress before saving on our controller.

@{
    ViewBag.Title = "Home Page";
}

@using (Html.BeginForm("Compression", "Home", FormMethod.Post, new {@enctype="multipart/form-data" }))
{
<input  type="file" id="file" name="file"/>
<input type="submit" value="Save"/>
}

Now, run your application and you will get option to select image. Now select any image file which is large in size and than click on save.You will notice the file saved in compressed folder is so much less in size than what we have upload. So, this is how we can Compress image using ImageCompress NuGet package in Asp.Net.




Comments

Tags

LinkedinLogin
LinkedinProfile
GetLinkedinProfile
C#
Aspnet
MVC
Linkedin
ITextSharp
Export to Pdf
AspNet Core
AspNet
View to Pdf in Aspnet
Model Validation In ASPNET Core MVC 60
Model Validation
Model Validation In ASPNET Core MVC
Model Validation In ASPNET
Image Compression in AspNet
Compress Image in c#
AspNet MVC
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1190
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook

Welcome To Code2night, A common place for sharing your programming knowledge,Blogs and Videos

  • Panipat
  • info@Code2night.com

Links

  • Home
  • Blogs
  • Tutorial
  • Post Blog

Popular Tags

Copyright © 2025 by Code2night. All Rights Reserved

  • Home
  • Blog
  • Login
  • SignUp
  • Contact
  • Terms & Conditions
  • Refund Policy
  • About Us
  • Privacy Policy
  • Json Beautifier
  • Guest Posts