Image compression is a popular way of reducing the size of the images. It lets you minimize the space and time required to store and transmit images respectively. Various compression techniques are available that compress images in such a way that the quality of the images is not degraded. In accordance with that, this post provides you with some simple ways of how to compress PNG, JPEG, and TIFF images programmatically using C#.
- C# Image Compression API – Free Download
- Compress PNG Images in C#
- Compress JPEG Images in C#
- Apply Compression to TIFF Images in C#
C# Image Compression API – Free Download
Aspose.Imaging for .NET is a powerful image processing API that lets you manipulate popular image formats from within your .NET applications. In addition, the API allows you to apply different types of compression to images including PNG, JPEG, and TIFF. In order to use the API, you can either download its DLL or install it using NuGet.
Install-Package Aspose.Imaging
Compress PNG Images in C#
For PNG images, you can set the compression level from 0 to 9 where 9 is the maximum compression and 0 is store mode. The following are the steps to compress a PNG image using Aspose.Imaging for .NET.
- Load the image using Image class.
- Create an object of PngOptions class.
- Set the compression level using PngOptions.CompressionLevel property.
- Save the image using Image.Save(String, PngOptions) method.
The following code sample shows how to compress PNG images using C#.
Compress JPEG Images in C#
In order to deal with JPEG images, Aspose.Imaging for .NET provides JpegOptions class which offers the following compression types for JPEG images.
- Baseline
- Progressive
- Lossless
- JpegLs
The following are the steps to compress JPEG images using one of the above-mentioned compression types.
- Load the JPEG image using Image class.
- Create an object of JpegOptions class.
- Set the color mode using JpegOptions.ColorType property.
- Set compression type using JpegOptions.CompressionType property.
- Save the image using Image.Save(String, JpegOptions) method.
The following code sample shows how to compress a JPEG image using C#.
Apply Compression on TIFF Images in C#
Aspose.Imaging for .NET provides a wide range of compression types for TIFF images including LZW, Packbits, CCIT Fax 3 & 4 and etc. You can select the appropriate type as per your requirements. The following are the steps to compress a TIFF image.
- Load the TIFF image using Image class.
- Create an object of TiffOptions and initialize it with TiffExpectedFormat.Default enum value.
- Set BitsPerSample, Compression, Photometric mode, and Palette for the Tiff image.
- Save the image using Image.Save(String, TiffOptions) method.
The following code sample shows how to compress TIFF images using C#.
Info: Using Aspose JPG to PPT or PNG to PPT converter, you can generate PowerPoint presentations from simple images.
Conclusion
In this post, you have learned how to compress PNG, JPEG, and TIFF images using C#. Various supported compression techniques have also been listed for JPEG and TIFF images. You can explore more about the .NET image processing API using documentation.