
How to Compress Images in C#
Integrate this .NET compression library to reduce popular image formats such as TIFF, JPG, PNG, and more. In large organizations, storage efficiency is critical, so you can use this library to compress images programmatically in a .NET application. Below you will find code snippets and steps for how to compress images in C#.
The guide covers:
- .NET compression library installation
- Compress JPG programmatically
- How to compress TIFF in C#
- How to reduce PNG file size?
.NET compression library installation
Installing the API is straightforward. Download its DLL or run the following command in the NuGet package manager.
Install-Package Aspose.Imaging
Compress JPG programmatically
Follow these steps to compress JPG images in C#:
- Create an Image object and call the Load method to open a JPG file.
- Instantiate a JpegOptions object.
- Set the desired ColorType for the image.
- Choose a CompressionType.
- Save the image with the Save method.
Copy & paste the code below into your project:
How to compress TIFF in C#
To compress a TIFF image, perform these actions:
- Load the TIFF file using the Load method.
- Create a TiffOptions instance with TiffExpectedFormat.Default.
- Set the BitsPerSample value.
- Define the Compression type.
- Adjust the Photometric and Palette properties as needed.
- Save the result with the Save method.
Copy & paste the code below into your project:
How to reduce PNG file size?
The .NET compression library provides simple methods to compress PNG files.
Steps:
- Load a PNG image with the Load method.
- Iterate over the desired CompressionLevel range.
- For each level, create a PngOptions object, set its CompressionLevel, and save the image.
- Compression levels range from 0 (no compression) to 9 (maximum compression).
- Use the save method to write the file.
Copy & paste the code below into your project:
Get a Free License
You can obtain a free temporary license to try the API without evaluation limits.
Summing up
This tutorial showed how to compress images in C# using the .NET compression library for PNG, TIFF, and JPG formats. For more details, visit the documentation.
Also, see our Getting Started guide for additional guidance.
Stay updated with new posts on conholdate.com.
Ask a question
Post your questions on our forum.
FAQs
How do I compress an image in .NET core?
Visit this link for code snippets and API methods provided by the .NET compression library.
How to compress PNG?
Create a PngOptions instance, set the CompressionLevel, and call the save method.