Skip to content

georg-jung/MozJpegSharp

This branch is 245 commits ahead of, 27 commits behind quamotion/AS.TurboJpegWrapper:master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

6b1c1c1 · Jan 6, 2023
Jan 3, 2023
Jan 6, 2023
Mar 12, 2020
Jan 6, 2023
Apr 27, 2020
Apr 27, 2020
Apr 27, 2020
Feb 18, 2020
Aug 9, 2021
Mar 12, 2020
Nov 7, 2021
Jan 4, 2023
Jan 4, 2023

Repository files navigation

MozJpegSharp logo

MozJpegSharp (.NET wrapper for MozJPEG)

NuGet version (MozJpegSharp) Build Status

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.

Installation

Install-Package MozJpegSharp
dotnet add package MozJpegSharp

Package Overview

This project provides multiple NuGet packages:

Package Description
MozJpegSharp Convenience package. Provides easy installation for MozJpegSharp.Core and MozJpegSharp.NativeAssets. All you need to recompress existing .jpg files on all supported platforms.
MozJpegSharp.Core Just the managed wrapper. Useful if you compile MozJPEG from source or get it from you OS package manager.
MozJpegSharp.NativeAssets Just the compiled native assets. Hardly useful solely installed.
MozJpegSharp.SkiaSharp Install besides MozJpegSharp. Extension methods for working with SKBitmap. Useful for compressing images in applications that already use SkiaSharp. Also great performance and cross platform compatibility. Good choice for new cross-platform projects that need to support more cases then just recompressing JPEGs.
MozJpegSharp.GdiPlus Install besides MozJpegSharp. Extension methods for working with System.Drawing.Bitmap objects. Useful for compressing any kind of image on Windows/.Net Framework.

Usage

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);

Credits

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.