@DanAvni
To achieve JPEG compression similar to what the mozjpeg library offers, you can adjust the quality settings and compression modes in Aspose.Imaging. By default, JPEG images are saved with a quality setting of 75, which can be increased to improve compression results.
Here’s an example of how to set the quality and compression type in Aspose.Imaging:
using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.ImageOptions;
public class JpegCompressionExample
{
public static void Main()
{
// Load the image
using (Image image = Image.Load("source_image.jpeg"))
{
// Create JPEG options
JpegOptions jpegOptions = new JpegOptions
{
CompressionType = JpegCompressionMode.Baseline, // or JpegCompressionMode.Progressive
Quality = 95 // Increase quality for better compression
};
// Save the image with the specified options
image.Save("compressed_image.jpeg", jpegOptions);
}
}
}
In this example, the quality is set to 95, which should help reduce the file size while maintaining image quality. You can experiment with different quality values to find the best balance for your needs.
Additionally, if you are using 4:2:0 chroma subsampling, you can specify the horizontal and vertical sampling factors as follows:
jpegOptions.HorizontalSampling = new byte[] { 2, 1, 1 };
jpegOptions.VerticalSampling = new byte[] { 2, 1, 1 };
This configuration can help achieve smaller file sizes similar to those produced by mozjpeg. If you continue to experience larger file sizes, consider adjusting the quality further or experimenting with different compression modes available in the library.
For more detailed examples and options, you can refer to the Aspose.Imaging documentation and sample code available on their GitHub repository.
Sources:
[1]: JPEG/JPG has less size after it’s stamped
[2]: OptimizationStrategyInJPEG.java