Reduce PNG file size using C#

Reduce PNG file size using C#

System storage is a critical concern in today’s technology landscape. Data volumes grow daily, and visual data files are increasingly popular. While many image compression methods exist, image quality must remain intact. We will use this .NET image compression API to compress PNG images. In this post, you will learn how to reduce PNG file size using C# programmatically.

We will cover:

PNG Image Compression - C# API Installation

This library provides methods and properties to manipulate image formats such as JPEG and TIFF. Install the API in your .NET project by either downloading the DLL files or using NuGet.

Install-Package Aspose.Imaging

How to Reduce PNG File Size using C#

A large set of methods and properties is available here for image compression. Follow these steps to compress a PNG file programmatically:

  1. Load the source PNG image with the Load method.
  2. Use the Save method to write the compressed image in PNG format.
  3. Set the Progressive property to define whether the PNG is progressive.
  4. Use IndexedColor to specify a palette‑indexed color type.
  5. Assign a value to CompressionLevel for maximum compression.
  6. Call GetCloseImagePalette to obtain a palette when the image lacks one; otherwise, the existing palette is used.

The snippet below demonstrates reducing PNG file size in C#:

PNG Image Compression - Advanced Options

After learning to reduce PNG file size using C#, explore additional methods and properties of the API. The following steps create a custom PNG file programmatically:

  1. Create a PngImage instance for a 100 × 100 px image.
  2. Apply a LinearGradientBrush for a linear gradient.
  3. Initialize a Point structure with the required coordinates.
  4. Use a Graphics object to draw on the image.
  5. Fill the image with a blue‑transparent gradient via FillRectangle.
  6. Create a PngOptions object.
  7. Set the Progressive property as needed.
  8. Define horizontal and vertical resolution (96 dpi) with ResolutionSetting.
  9. Choose a color type using the ColorType property.
  10. Set the CompressionLevel for maximum compression.
  11. Specify the FilterType used during saving.
  12. Adjust bits per channel with the BitDepth property.
  13. Save the file using the Save method.

The following sample code shows how to compress PNG images in C#:

The result is displayed below:

image compression API

Get a Free License

Obtain a free temporary license to try the API without evaluation limits.

Summing up

We have shown how to reduce PNG file size using C# programmatically and explored advanced features of the PNG image compression API. For more methods and properties, visit this link. Always review the documentation of the .NET library for full capabilities. Stay tuned to conholdate.com for future updates.

Ask a question

Post 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 files. It also supports formats like JPEG and TIFF.

How to minimize image file size for PNG format?
Use the method to reduce PNG file size with C#. You can customize the API call to meet your needs.

How to resize a PNG image in C# code example?
See this link for a code snippet that resizes a PNG image programmatically. The .NET API offers extensive image processing features.

See Also