Code2night
  • Home
  • Guest Posts
  • Tutorial
  • Languages
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
  • Register
  • Login
  1. Home
  2. Blogpost

Compress image using ImageCompress NuGet package

Date- Jul 22,2022

6911

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.




S
Shubham Batra
Programming author at Code2Night โ€” sharing tutorials on ASP.NET, C#, and more.
View all posts โ†’

Comments

Tags

AspNet
C#
programming
AspNet MVC
c programming
AspNet Core
C
software development
tutorial
MVC
memory management
Paypal
coding
coding best practices
data structures
programming tutorial
tutorials
object oriented programming
Slick Slider
StripeNet
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 | 1760
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
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blogs
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
By Language
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ยท  Terms
Translate Page