Reduce PNG file size using C#

Reduce PNG file size using C#

Managing storage space is a top priority for modern applications as data volumes, especially image files, continue to surge. Maintaining visual quality while shrinking file size is essential. This guide uses the .NET image compression API to compress PNG images efficiently. You’ll learn step‑by‑step how to reduce PNG file size using C# programmatically.

We will cover:

PNG Image Compression - C# API Installation

The Aspose.Imaging library offers comprehensive methods for handling formats such as JPEG and TIFF. Install the library in your .NET project via NuGet or by downloading the DLLs.

Install-Package Aspose.Imaging

How to Reduce PNG File Size using C#

Aspose.Imaging provides a rich set of functions for PNG compression. Follow these concise 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

Beyond basic compression, the Aspose.Imaging API offers advanced settings to fine‑tune PNG output. The steps below demonstrate how to create a custom PNG with optimized options:

  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

Request a free temporary license to evaluate the API without usage limits.

Summing up

In this article we demonstrated how to reduce PNG file size using C# programmatically and explored advanced features of the PNG image compression API. For a full list of methods and properties, visit this link. Review the official documentation for detailed guidance, and stay updated via conholdate.com.

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