This quick tutorial shares the information on how to compress image size in C#. A few lines of code demonstrated in C# compress image of any type like JPEG, PNG, BMP, etc. such that you get complete control over the compression process. For this purpose, different properties are provided to compress the images without losing quality.
Steps to Compress Image Size in C#
- Configure the project to use Aspose.Imaging for compressing the JPEG image
- Load the source JPEG file into the Image class object
- Create and initialize the JpegOptions class object to customize the compression
- Set color type to Grayscale to reduce the image size
- Set compression type to progressive
- Save the source image on the disk with compression settings given above
To compress image size C# environment configuration and sequence of operations is described in the above steps. All the necessary classes are identified like the source image is loaded into the Image class. Similarly, the JpegOptions class object is used to configure the output compressed image along with the usage of a couple of properties that are set in the sample code.
Code to Compress Image Size in C#
The above program statements in C# compress image size by using JpegOptions where color type is set to Grayscale and CompressionType is set to Progressive. There are a number of other properties available also like Palette, Quality, flag for full-frame, multi-page options, horizontal sampling, and vertical sampling to name a few. If you want to compress a PNG image, you may use PNGOptions, and for TIFF images use TiffOptions.
In this tutorial, we have learned to compress image size in C# If you want to resize image, refer to the article on how to resize image in C#.