How to Compress a PNG in C#

This example covers the details of how to Compress a PNG in C#. It covers all the resources, namespaces, classes, methods and working example code to compress PNG using C#. The developed application can be utilized in any of .NET compliant environment inside Windows, Linux or macOS.

Steps to Compress PNG using C#

  1. Establish the environment by adding Aspose.Imaging using the NuGet package manager
  2. Access the source PNG image using an instance of the Image class
  3. Use the PngOptions class to set the compression ratio along with other properties
  4. Save the compressed PNG image on the disk

By following the above mentioned steps, you can easily manage PNG compression in C# using a step-by-step approach where we set up the environment by including the required resources in the solution. We will then load the source PNG file from the disk by using an instance of the Image class. In the subsequent steps, PngOptions class object will be created to set the compression ratio along with other properties, which will then be followed by saving the compressed PNG on the disk.

Code to Compress PNG using C#

The above example can be used for compressing PNG file in C# using a very simple API interface. The Image class exposes multiple overload functions that you can use to load the images from a stream along with other additional LoadOptions parameters to support other types of images. The PngOptions class object exposes the properties to set the color type, compression ratio, vector rasterization options etc, to name a few.

This topic has taught us to develop an application that can easily perform PNG compression using C#. If you want to learn about converting a PNG to ICON, refer to the article on how to convert PNG to ICON in C#.

 English