How to Compress Images in C#

How to Compress Images in C#

Integrate the Aspose.Imaging .NET compression library to shrink popular image formats such as TIFF, JPG and PNG. In enterprise environments, reducing file size saves storage costs and speeds up image delivery. This guide shows you step‑by‑step how to compress images in C# with ready‑to‑use code snippets and best‑practice recommendations.

The tutorial includes:

  • Detailed .NET compression library installation instructions
  • Complete example for compressing JPG files programmatically6
  • Proven method to compress TIFF images in C#7
  • Efficient technique to reduce PNG file size8

.NET compression library installation

Install Aspose.Imaging via NuGet or by downloading the DLL. Using the NuGet Package Manager Console ensures you get the latest stable release:

Install-Package Aspose.Imaging

After installation, add a reference to Aspose.Imaging.dll in your project to start using the API.

Compress JPG programmatically

Follow these steps to compress JPG images while preserving visual quality:

  1. Create an Image object and call the Load method to open the JPG file.
  2. Instantiate a JpegOptions object.
  3. Set the desired ColorType (e.g., Rgb) for the output.
  4. Choose a CompressionType such as Baseline.
  5. Adjust the Quality property (0‑100) to balance size and quality.
  6. Save the image with the Save method.

Copy & paste the code below into your project:

How to compress TIFF in C#

Compressing TIFF files requires configuring several TIFF‑specific options:

  1. Load the TIFF file using the Load method.
  2. Create a TiffOptions instance with TiffExpectedFormat.Default.
  3. Set the BitsPerSample to match your desired color depth.
  4. Define the Compression type (e.g., Lzw or Deflate).
  5. Optionally adjust the Photometric and Palette settings for optimal results.
  6. Save the compressed image with the Save method.

Copy & paste the code below into your project:

How to reduce PNG file size?

The Aspose.Imaging .NET compression library offers a straightforward way to compress PNG images without losing quality:

  1. Load a PNG image with the Load method.
  2. Loop through the desired CompressionLevel range (0‑9).
  3. For each level, create a PngOptions object, set its CompressionLevel, and save the image.
  4. Higher levels (8‑9) provide maximum loss‑less compression, while lower levels keep processing fast.
  5. Use the Save method to write the compressed file to disk.

Copy & paste the code below into your project:

Get a Free License

You can obtain a free temporary license to try the API without evaluation limits.

Summing up

This tutorial demonstrated how to compress images in C# using the Aspose.Imaging .NET compression library for JPG, TIFF, and PNG formats. For deeper details, explore the full documentation and the Getting Started guide.

Stay updated with new posts on conholdate.com.

Ask a question

Post your questions on our forum.

FAQs

How do I compress an image in .NET core?
Visit this link for code snippets and API methods provided by the .NET compression library.

How to compress PNG?
Create a PngOptions instance, set the CompressionLevel, and call the Save method.

See Also