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
- How to Reduce PNG File Size using C#
- PNG Image Compression - Advanced Options
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:
- Load the source PNG image by calling the Load method.
- Invoke the Save method to save the compressed image in PNG format.
- Set a value of Progressive property that indicates whether this PngOptions is progressive.
- IndexedColor represents the color type where each pixel is a palette index.
- Assign the value of CompressionLevel to set the maximum compression.
- 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#:
// Load the source PNG image by calling the Load method | |
using (Image image = Image.Load("image.png")) | |
{ | |
// Invoke the Save method to save the compressed image in PNG format | |
image.Save(out.png, new ImageOptions.PngOptions() | |
{ | |
// Set a value of Progressive property that indicates whether this PngOptions is progressive. | |
Progressive = true, | |
// IndexedColor represents the color type where each pixel is a palette index | |
ColorType = FileFormats.Png.PngColorType.IndexedColor, | |
// Assign the value of CompressionLevel to set the maximum compression | |
CompressionLevel = 9, | |
// GetCloseImagePalette method will get color palette from raster image (palletizes image) in case the image does not have one. In case palette exists it will be used instead performing calculations. | |
Palette = ColorPaletteHelper.GetCloseImagePalette((RasterImage)image, 256, PaletteMiningMethod.Histogram) | |
}); | |
} |
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:
- Initialize an instance of PngImage class that creates a PNG image of 100x100 px.
- The LinearGradientBrush method encapsulates a Brush with a linear gradient.
- Initializes a new instance of the Point structure with the specified coordinates.
- Instantiate an instance of the Graphics class that represents the graphics according to the graphics engine used in the current assembly.
- Fill the image with the blue-transparent gradient with the FillRectangle method.
- Create an object of the PngOptions class.
- Set a value of Progressive property that indicates whether this PngOptions is progressive.
- You can set the horizontal and vertical resolution to 96 pixels per inch by initializing the ResolutionSetting class.
- Set the type of color by assigning a value to the ColorType property.
- Assign a value to the CompressionLevel property to set the maximum level of compression.
- FilterType property sets the filter type used during the PNG file save process.
- Set the number of bits per channel by assigning a value to the BitDepth property.
- Save to a file by calling the Save method.
The following sample code demonstrates how to compress PNG image in C#:
// Initialize an instance of PngImage class that creates a PNG image of 100x100 px. | |
using (Aspose.Imaging.FileFormats.Png.PngImage pngImage = new Aspose.Imaging.FileFormats.Png.PngImage(100, 100)) | |
{ | |
// LinearGradientBrush method encapsulates a Brush with a linear gradient | |
Aspose.Imaging.Brushes.LinearGradientBrush gradientBrush = new Aspose.Imaging.Brushes.LinearGradientBrush( | |
// Initializes a new instance of the Point structure with the specified coordinates. | |
new Aspose.Imaging.Point(0, 0), | |
new Aspose.Imaging.Point(pngImage.Width, pngImage.Height), | |
Aspose.Imaging.Color.Blue, | |
Aspose.Imaging.Color.Transparent); | |
// Intantiate an instance of Graphics class that represents the graphics according to the graphics engine used in the current assembly. | |
Aspose.Imaging.Graphics graphics = new Aspose.Imaging.Graphics(pngImage); | |
// Fill the image with the blue-transparent gradient with FillRectangle method . | |
graphics.FillRectangle(gradientBrush, pngImage.Bounds); | |
// Create an object of the PngOptions class | |
Aspose.Imaging.ImageOptions.PngOptions saveOptions = new Aspose.Imaging.ImageOptions.PngOptions(); | |
// sets a value of Progressive property that indicates whether this PngOptions is progressive. | |
saveOptions.Progressive = true; | |
// Set the horizontal and vertical resolution to 96 pixels per inch by initializing the ResolutionSetting class. | |
saveOptions.ResolutionSettings = new Aspose.Imaging.ResolutionSetting(96.0, 96.0); | |
// set the type of the color by assiging a value to the ColorType property . | |
saveOptions.ColorType = Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha; | |
// Assign a value to the CompressionLevel property to set the maximum level of compression. | |
saveOptions.CompressionLevel = 9; | |
// FilterType property sets the filter type used during png file save process. | |
saveOptions.FilterType = Aspose.Imaging.FileFormats.Png.PngFilterType.Adaptive; | |
// set the number of bits per channel by assiging a value to the BitDepth property. | |
saveOptions.BitDepth = 8; | |
// Save to a file by calling the Save method. | |
pngImage.Save( "output.png", saveOptions); | |
} |
You can see the output in the image below:
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.