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

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.

The following code sample shows how to compress PNG images using C#.

// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_PNG();
// Load an image from file (or stream)
using (Image image = Image.Load(dataDir + "aspose_logo.png"))
{
// Loop over possible CompressionLevel range
for (int i = 0; i <= 9; i++)
{
// Create an instance of PngOptions for each resultant PNG, Set CompressionLevel and Save result on disk
PngOptions options = new PngOptions();
options.CompressionLevel = i;
image.Save(i + "_out.png", options);
}
}

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.

The following code sample shows how to compress a JPEG image using C#.

// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_JPEG();
using (var original = Image.Load(dataDir+"ColorGif.gif"))
{
var jpegOptions = new JpegOptions()
{
ColorType = JpegCompressionColorMode.Grayscale,
CompressionType = JpegCompressionMode.Progressive,
};
original.Save("D:/temp/result.jpg", jpegOptions);
}

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.

The following code sample shows how to compress TIFF images using C#.

// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_ModifyingAndConvertingImages();
// Load an image through file path location or stream
Image image = Image.Load(dataDir + "SampleTiff.tiff");
// Create an instance of TiffOptions for the resultant image
TiffOptions outputSettings = new TiffOptions(TiffExpectedFormat.Default);
// Set BitsPerSample, Compression, Photometric mode and graycale palette
outputSettings.BitsPerSample = new ushort[] { 4 };
outputSettings.Compression = TiffCompressions.Lzw;
outputSettings.Photometric = TiffPhotometrics.Palette;
outputSettings.Palette = ColorPaletteHelper.Create4BitGrayscale(false);
image.Save(dataDir + "SampleTiff_out.tiff", outputSettings);

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.

See Also

Info: Using Aspose JPG to PPT or PNG to PPT converter, you can generate PowerPoint presentations from simple images.