Reduce PNG file size using C#

Reduce PNG file size using C#

System storage is a critical subject in this modern age of technology. The amount of data is increasing every day and the visual representation of data files has gained lots of popularity. However, there are many ways to compress image file formats but the image quality should not be compromised. Therefore, we will use this .NET image compression API to compress the PNG images. In this blog post, we will learn how to reduce PNG file size using C# programmatically.

We will cover the following points in this article:

PNG Image Compression - C# API Installation

This library offers a wide range of methods and properties to manipulate and process various image file formats such as JPEG, TIFF, and more. There are two ways to install this API into your .NET project.

Therefore, you can either download the DLL files or run the following command in the NuGet package manager.

Install-Package Aspose.Imaging

How to Reduce PNG File Size using C#

You can see a huge stack of methods and properties here to compress the image file formats. Therefore, we will write steps and the code snippet to compress the PNG file programmatically.

Follow the following steps:

  1. Load the source PNG image by calling the Load method.
  2. Invoke the Save method to save the compressed image in PNG format.
  3. Set a value of Progressive property that indicates whether this PngOptions is progressive.
  4. IndexedColor represents the color type where each pixel is a palette index.
  5. Assign the value of CompressionLevel to set the maximum compression.
  6. GetCloseImagePalette method will get a color palette from the raster image (palletizes image) in case the image does not have one. In case a palette exists it will be used instead of performing calculations.

The code snippet below shows how to reduce PNG file size in C#:

PNG Image Compression - Advanced Options

In the previous section, we learned how to reduce PNG file size using C#. Now, let’s explore some other prominent methods and properties of this image compression API. In this section, we will make a custom API call to create a PNG file programmatically.

You may follow the steps and the code snippet mentioned below:

  1. Initialize an instance of PngImage class that creates a PNG image of 100x100 px.
  2. The LinearGradientBrush method encapsulates a Brush with a linear gradient.
  3. Initializes a new instance of the Point structure with the specified coordinates.
  4. Instantiate an instance of the Graphics class that represents the graphics according to the graphics engine used in the current assembly.
  5. Fill the image with the blue-transparent gradient with the FillRectangle method.
  6. Create an object of the PngOptions class.
  7. Set a value of Progressive property that indicates whether this PngOptions is progressive.
  8. You can set the horizontal and vertical resolution to 96 pixels per inch by initializing the ResolutionSetting class.
  9. Set the type of color by assigning a value to the ColorType property.
  10. Assign a value to the CompressionLevel property to set the maximum level of compression.
  11. FilterType property sets the filter type used during the PNG file save process.
  12. Set the number of bits per channel by assigning a value to the BitDepth property.
  13. Save to a file by calling the Save method.

The following sample code demonstrates how to compress PNG image in C#:

You can see the output in the image below:

image compression API

Get a Free License

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

Summing up

We can end this blog post here. We hope you have learned how to reduce PNG file size using C# programmatically. In addition, we have gone through some other advanced methods of this PNG image compression API where we have created an optimized PNG file. However, you can visit this link for further methods and properties regarding the PNG file format. Above all, do not skip the documentation of this .NET library to know the ecosystem. We will write on other useful topics in future blog posts. Therefore, stay in touch with conholdate.com for updates.

Ask a question

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

FAQs

How to compress PNG image in C#?

Install this .NET image compression API to compress and manipulate PNG file formats. It offers methods and properties to compress other popular image file formats such as JPEG, TIFF, and more.

How to minimize image file size for PNG format?

This method lets you reduce the PNG file size using C# programmatically. Moreover, you can configure the API call as per your requirements.

How to resize a PNG image in C# code example?

Follow this link to learn about the code snippet that resizes a PNG image programmatically. This .NET API is powerful and offers a wide range of methods & properties.

See Also