How to Compress Images in C#

How to Compress Images in C#

Integrate this .NET compression library to compress the popular image file formats such as TIFF, JPG, PNG, and more. In huge organizations, file storage is a critical subject and you always tend to optimize your business files and make their size memory-efficient. Therefore, you can use this library to compress images programmatically in a .NET application. In addition, you will also get the code snippets and the steps about how to compress images in C#|.

The following points will be covered in this guide:

.NET compression library installation

As far as the installation of this API is concerned, it is super simple. You can either download its DLL or install it by running the following command in the NuGet package manager.

Install-Package Aspose.Imaging

Compress JPG programmatically

Now, we will implement how to compress the images in C# programmatically. You may follow the steps and the code snippet mentioned below:

  1. Instantiate an instance of Image class and call the Load method to load a JPG image.
  2. Initialize an object of the JpegOptions class.
  3. Set the color type for the JPEG image by setting the value of the ColorType property.
  4. Specify the compression type by setting the value of the CompressionType property.
  5. The Save method will save the image.

Copy & paste the following code into your main file:

How to compress TIFF in C#

In order to compress a TIFF image, you need to follow the following steps:

Following are the steps:

  1. Invoke the Load method to load a TIFF image.
  2. Initialize the constructor of TiffOptions class with TiffExpectedFormat.Default value.
  3. Set the value of the BitsPerSample property.
  4. Also, set the value of the Compression property.
  5. Finally, set the Photometric, and Palette properties.
  6. Call the Save method to save the image.

Copy & paste the following code into your main file:

How to reduce PNG file size?

This .NET compression library offers a wide range of methods to manipulate and compress PNG files easily.

Following are the steps and the code snippet to compress PNG programmatically:

  1. Load a PNG image by calling the Load method.
  2. Loop over possible CompressionLevel range.
  3. Create an instance of PngOptions class for each resultant PNG, Set CompressionLevel, and Save the result on the disk.
  4. The PNG image compression level is in the 0-9 range, where 9 is maximum compression and 0 is store mode. Set the value of the CompressionLevel property.
  5. Invoke the save method to save the image.

Copy & paste the following code into your main file:

Get a Free License

You can avail a free temporary license to try the API without evaluation limitations.

Summing up

We can conclude this tutorial blog post with the hope that you have learned how to compress images in C#. Moreover, you have gone through this .NET compression library to compress PNG, TIFF, and JPG image file formats programmatically. Most importantly, you may visit the documentation to know about other features.

Moreover, we suggest you follow our Getting Started guide.

Finally, conholdate.com is writing new blog posts. Therefore, please stay in touch for regular updates.

Ask a question

You can let us know about your questions or queries on our forum.

FAQs

How do I compress an image in .NET core?

Please visit this link to know the code snipets and API methods exposed by this .NET compression library.

How to compress PNG?

Create an instance of PngOptions for each resultant PNG, set the value of the CompressionLevel property and invoke the save method to save the image.

See Also