Shan.ImageCompressor 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Shan.ImageCompressor --version 1.1.0
                    
NuGet\Install-Package Shan.ImageCompressor -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Shan.ImageCompressor" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Shan.ImageCompressor" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Shan.ImageCompressor" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Shan.ImageCompressor --version 1.1.0
                    
#r "nuget: Shan.ImageCompressor, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Shan.ImageCompressor@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Shan.ImageCompressor&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Shan.ImageCompressor&version=1.1.0
                    
Install as a Cake Tool

Shan.ImageCompressor

Compress JPEG, PNG, GIF and BMP images toward a target file size. No dependencies — the image codecs are part of the package, so there is no third-party imaging library (and no third-party licence) to worry about.

  • Detects the real format from the bytes, not the client-claimed content type
  • Opaque images become JPEG (quality stepped down, then dimensions); images with real transparency stay PNG
  • Applies EXIF orientation, and drops all metadata (EXIF/GPS/XMP/IPTC) — a decoded-and-re-encoded image carries none
  • Images already within the target size are returned untouched
  • Refuses oversized images (MaxPixels) before allocating pixel memory, so a small file can't expand into gigabytes
using Shan.ImageCompressor;

await using var file = File.OpenRead("photo.jpg");
var result = await ImageCompressor.CompressAsync(file, new ImageCompressionOptions { TargetSizeBytes = 500 * 1024 });

await using var output = File.Create("photo" + result.Extension);
await result.Content.CopyToAsync(output);

There is a synchronous version too, for code that isn't async: ImageCompressor.Compress(stream, options) or ImageCompressor.Compress(bytes, options). Unusable options (a ShrinkFactor of 1, a MinWidth of 0, …) throw ArgumentOutOfRangeException before any work is done.

What it can read

Format Detect Compress
PNG yes yes — every colour type and bit depth, Adam7 interlacing, tRNS transparency
JPEG yes baseline, extended-sequential and progressive, 8-bit, grayscale or YCbCr/RGB, any chroma subsampling, restart intervals
GIF yes yes — the first frame, with interlacing and the transparent colour; an animated GIF is not flattened (see below)
BMP yes yes — 1/4/8-bit palette, 16/24/32-bit, bit masks (incl. alpha), RLE4/RLE8, top-down or bottom-up
WebP yes no

An image that is already small enough is returned as-is whatever its format. One that needs compressing but isn't a supported kind throws NotSupportedException (WebP, animated GIF, CMYK or arithmetic-coded JPEG); a corrupt or non-image stream throws InvalidDataException — and nothing else, however damaged the bytes are. An animated GIF is rejected rather than squashed into a still, so a caller can keep the original, as with any other unsupported file.

Output is always JPEG (baseline, 4:2:0 chroma below quality 90, 4:4:4 from 90 up) or PNG (8-bit RGB/RGBA). Compare against libjpeg at the same quality: file sizes land within about 2%.

Notes

  • Compression is CPU-bound and synchronous after the input is read; a 12-megapixel photo takes a few seconds. Run it off a request thread if that matters, and pass a CancellationToken to stop between passes.
  • A decoded image takes 4 bytes per pixel. MaxPixels (default 50 million) caps that; lower it if you run in a small container.
  • Not a general-purpose imaging library: no editing, no colour management (ICC profiles are dropped, pixels are assumed sRGB).
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.2 92 11/26/2026
2.0.1 91 11/25/2026
2.0.0 90 11/24/2026
1.1.0 81 11/24/2026
1.0.1 87 11/24/2026
1.0.0 90 11/24/2026

Adds progressive JPEG decoding, GIF (first frame; animated GIFs are rejected, not flattened) and BMP input, synchronous Compress overloads and option validation, and fixes a raw ZLibException on damaged PNG data. Full notes: https://github.com/bdshanto/Shan.ImageCompressor/blob/main/CHANGELOG.md