Integrate this .NET compression library to compress the popular image file formats such as TIFF, JPG, PNG, and more. In huge organizations, file storage is a critical subject and you always tend to optimize your business files and make their size memory-efficient. Therefore, you can use this library to compress images programmatically in a .NET application. In addition, you will also get the code snippets and the steps about how to compress images in C#|.
The following points will be covered in this guide:
- .NET compression library installation
- Compress JPG programmatically
- How to compress TIFF in C#
- How to reduce PNG file size?
.NET compression library installation
As far as the installation of this API is concerned, it is super simple. You can either download its DLL or install it by running the following command in the NuGet package manager.
Install-Package Aspose.Imaging
Compress JPG programmatically
Now, we will implement how to compress the images in C# programmatically. You may follow the steps and the code snippet mentioned below:
- Instantiate an instance of Image class and call the Load method to load a JPG image.
- Initialize an object of the JpegOptions class.
- Set the color type for the JPEG image by setting the value of the ColorType property.
- Specify the compression type by setting the value of the CompressionType property.
- The Save method will save the image.
Copy & paste the following code into your main file:
// Instantiate an instance of Image and class and call the Load method to load a JPG image | |
using (var original = Image.Load( "sample.jpg")) | |
{ | |
// Initialize an object of the JpegOptions class | |
var jpegOptions = new JpegOptions() | |
{ | |
// Set the color type for jpeg image by setting the value of the ColorType property. | |
ColorType = JpegCompressionColorMode.Grayscale, | |
// Specify the compression type by setting the value of CompressionType property | |
CompressionType = JpegCompressionMode.Progressive, | |
}; | |
// Save method will save the image. | |
original.Save( "result.jpg", jpegOptions); | |
} |
How to compress TIFF in C#
In order to compress a TIFF image, you need to follow the following steps:
Following are the steps:
- Invoke the Load method to load a TIFF image.
- Initialize the constructor of TiffOptions class with TiffExpectedFormat.Default value.
- Set the value of the BitsPerSample property.
- Also, set the value of the Compression property.
- Finally, set the Photometric, and Palette properties.
- Call the Save method to save the image.
Copy & paste the following code into your main file:
// Invoke the Load method to load a TIFF image | |
using (Image image = Image.Load( "sample.tiff")) | |
{ | |
// Initialize the constructor of TiffOptions class with TiffExpectedFormat.Default value. | |
TiffOptions outputSettings = new TiffOptions(TiffExpectedFormat.Default); | |
// Set the value of the BitsPerSample property | |
outputSettings.BitsPerSample = new ushort[] { 4 }; | |
// Also, set the value of the Compression property | |
outputSettings.Compression = TiffCompressions.Lzw; | |
// Finally set the Photometric, Palette properties | |
outputSettings.Photometric = TiffPhotometrics.Palette; | |
outputSettings.Palette = ColorPaletteHelper.Create4BitGrayscale(false); | |
// Call the Save method to save the image. | |
image.Save( "result.tiff", outputSettings); | |
} |
How to reduce PNG file size?
This .NET compression library offers a wide range of methods to manipulate and compress PNG files easily.
Following are the steps and the code snippet to compress PNG programmatically:
- Load a PNG image by calling the Load method.
- Loop over possible CompressionLevel range.
- Create an instance of PngOptions class for each resultant PNG, Set CompressionLevel, and Save the result on the disk.
- The PNG image compression level is in the 0-9 range, where 9 is maximum compression and 0 is store mode. Set the value of the CompressionLevel property.
- Invoke the save method to save the image.
Copy & paste the following code into your main file:
// Load an image from file by calling the Load method | |
using (Image image = Image.Load( "result.png")) | |
{ | |
// Loop over possible CompressionLevel range | |
for (int i = 0; i <= 9; i++) | |
{ | |
// Create an instance of PngOptions for each resultant PNG, Set CompressionLevel and Save result on disk | |
PngOptions options = new PngOptions(); | |
// The png image compression level in the 0-9 range, where 9 is maximum compression and 0 is store mode. Set the value of CompressionLevel property. | |
options.CompressionLevel = i; | |
// Invoke the save method to save the image | |
image.Save(i + "_dam.png", options); | |
} | |
} |
Get a Free License
You can avail a free temporary license to try the API without evaluation limitations.
Summing up
We can conclude this tutorial blog post with the hope that you have learned how to compress images in C#. Moreover, you have gone through this .NET compression library to compress PNG, TIFF, and JPG image file formats programmatically. Most importantly, you may visit the documentation to know about other features.
Moreover, we suggest you follow our Getting Started guide.
Finally, conholdate.com is writing new blog posts. Therefore, please stay in touch for regular updates.
Ask a question
You can let us know about your questions or queries on our forum.
FAQs
How do I compress an image in .NET core?
Please visit this link to know the code snipets and API methods exposed by this .NET compression library.
How to compress PNG?
Create an instance of PngOptions for each resultant PNG, set the value of the CompressionLevel property and invoke the save method to save the image.