Share via

How to reduce JPEG file size after compressing images using C# System.Drawing in .NET 6?

Shakeel khan 25 Reputation points
2026-05-11T05:37:43.6833333+00:00

I am building a web application on Azure App Service using .NET 6 and the System.Drawing.Common library.

I need to upload and compress multiple JPEG images that users submit via a form. Currently, I am using the Image.Save() method with Encoder.Quality set to 50%, but the output file size is still too large (around 2-3 MB per image). Users need to upload 20-30 images at once.

My constraints:

  • I cannot use paid third-party APIs.
  • I need to keep the original dimensions (resizing is not an option).
  • The solution must run server-side on Azure.

**
What I have tried already:**

csharp

var encoderParameters = new EncoderParameters(1);

encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 50L);

image.Save(outputStream, GetEncoderInfo("image/jpeg"), encoderParameters);

This reduces quality but not enough in terms of file size. Some images still remain above 1.5 MB.

My question: Is there a way in .NET to further reduce JPEG file size without affecting dimensions or noticeable visual quality? Alternatively, are there any lightweight command-line tools or web services that can batch compress JPEGs effectively?

I found an external tool that seems to do this, but I want to implement it programmatically within C# if possible. Any advice would be appreciated.

Developer technologies | C#
Developer technologies | C#

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.


Answer accepted by question author

Nancy Vo (WICLOUD CORPORATION) 4,765 Reputation points Microsoft External Staff Moderator
2026-05-11T08:10:22.8633333+00:00

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:

  1. Installation
Install-Package SixLabors.ImageSharp
  1. 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.

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.