MozJPEG reduces file sizes of JPEG images while retaining quality and compatibility with the vast majority of the world's deployed decoders.
This library provides .NET wrappers for MozJPEG, allowing you to use it from any .NET language, such as C#.
We also provide precompiled binaries of MozJPEG for Windows (x86/x64), Linux x64 and macOS. On these platforms you just need to install the package MozJpegSharp
to start producing smaller JPEGs.
Install-Package MozJpegSharp
dotnet add package MozJpegSharp
This project provides multiple NuGet packages:
Recompression using just MozJpegSharp
:
var inFile = "in.jpg";
var outFile = "out.jpg";
var inBytes = await File.ReadAllBytesAsync(inFile);
var compressed = MozJpeg.Recompress(inBytes.AsSpan(), quality: 80);
await File.WriteAllBytesAsync(outFile, compressed);
Example usage for MozJpegSharp.GdiPlus
:
var inFile = "in.jpg"; // does not need to be jpg
var outFile = "out.jpg";
using var bmp = new Bitmap(file);
using var tjc = new MozJpegSharp.TJCompressor();
var compressed = tjc.Compress(bmp, MozJpegSharp.TJSubsamplingOption.Chrominance420, 75, MozJpegSharp.TJFlags.None);
File.WriteAllBytes(outFile, compressed);
The code for the wrapper library is based on Quamotion.TurboJpegWrapper
. Most credits go to Quamotion. This project replaces libjpeg-turbo with mozjpeg to achieve higher compression rates.