C# Image Compression API - Free Download
for .NETis a powerful image processing API that allows you to process common image formats in .NET applications. Additionally, the API allows you to apply different types of compression to images, including PNG, JPEG, and TIFF. To use the API, you can download its DLL or install it using NuGet.
Install-Package
Compress PNG images in C#
For PNG images, you can set the compression level to 0 to 9, where 9 is the maximum compression and 0 is the storage mode. Here are the steps to compress PNG images using for .NET.
- Use the Image class to load the image.
- Create an object of the PngOptions class.
- Use properties to set the compression level.
- Use the (String, PngOptions) method to save the image.
The following code example shows how to compress PNG images using C#.
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_PNG(); // Load an image from file (or stream) using (Image image = (dataDir + "aspose_logo.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(); = i; (i + "_out.png", options); } }
Compressing JPEG images in C#
To handle JPEG images, for .NET provides the JpegOptions class, which provides the following compression types for JPEG images.
- Baseline
- progress
- No loss
- JPEG format
Here are the steps to compress a JPEG image using one of the compression types mentioned above.
- Loading JPEG images using the Image class.
- Create an object of the JpegOptions class.
- Use properties to set the color mode.
- Use properties to set the compression type.
- Use the (String, JpegOptions) method to save the image.
The following code example shows how to compress JPEG images using C#.
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_JPEG(); using (var original = (dataDir+"")) { var jpegOptions = new JpegOptions() { ColorType = , CompressionType = , }; ("D:/temp/", jpegOptions); }
Apply compression to TIFF images in C#
for .NET provides a variety of compression types for TIFF images, including LZW, Packbits, CCIT Fax 3 & 4, and more. You can choose the appropriate type as you want. Here are the steps to compress a TIFF image.
for .NET provides a variety of compression types for TIFF images, including LZW, Packbits, CCIT Fax 3 & 4, and more. You can choose the appropriate type as you want. Here are the steps to compress a TIFF image.
- Load TIFF images using the Image class.
- Create a TiffOptions object and initialize it with an enumeration value.
- Set BitsPerSample, compression, photometric modes, and color palette for Tiff images.
- Use the (String, TiffOptions) method to save the image.
The following code example shows how to compress TIFF images using C#.
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_ModifyingAndConvertingImages(); // Load an image through file path location or stream Image image = (dataDir + ""); // Create an instance of TiffOptions for the resultant image TiffOptions outputSettings = new TiffOptions(); // Set BitsPerSample, Compression, Photometric mode and graycale palette = new ushort[] { 4 }; = ; = ; = ColorPaletteHelper.Create4BitGrayscale(false); (dataDir + "SampleTiff_out.tiff", outputSettings);
in conclusion
In this article, you learned how to compress PNG, JPEG, and TIFF images using C#. Various compression technologies supported by JPEG and TIFF images are also listed. You can use the documentation to learn more about the .NET Image Processing API.
The above is the detailed content of the method of using C# to compress PNG, JPEG and TIFF images. For more information about C# to compress PNG, JPEG and TIFF, please follow my other related articles!