An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
Hello @Shakeel khan ,
Thanks for your question.
System.Drawing JPEG encoder at Quality 50% is not efficient enough, it doesn't compress as well as modern image libraries, even at the same quality setting.
I recommend using ImageSharp since it produces significantly smaller files at the exact same quality level.
You can refer to following steps:
- Installation
Install-Package SixLabors.ImageSharp
- Code example
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Jpeg;
public class ImageCompressor
{
public static async Task<byte[]> CompressJpegAsync(Stream inputStream, int quality = 50)
{
using var image = await Image.LoadAsync(inputStream);
image.Metadata.ExifProfile = null;
image.Metadata.IptcProfile = null;
image.Metadata.XmpProfile = null;
var encoder = new JpegEncoder
{
Quality = quality,
ColorType = JpegColorType.YCbCrRatio420
};
using var outputStream = new MemoryStream();
await image.SaveAsJpegAsync(outputStream, encoder);
return outputStream.ToArray();
}
public async Task<List<byte[]>> BatchCompressAsync(List<Stream> imageStreams, int quality = 50)
{
var results = new List<byte[]>();
foreach (var chunk in imageStreams.Chunk(5))
{
var tasks = chunk.Select(stream => CompressJpegAsync(stream, quality));
var compressed = await Task.WhenAll(tasks);
results.AddRange(compressed);
}
return results;
}
}
For more information on how to use, you can refer to this main document.
While this is a non-Microsoft link, it’s official SIX LABORS documentation and is safe to visit.
I hope this addresses your question. If this response was helpful, please consider following the guidance to provide feedback.