---
title: "Set PNG compression level in C#"
url: https://recursiveai.org/oneimage/cookbook/set-png-compression-level/
description: "Set PNG compression from 0 to 9 in C# with OneImage. Encode the same image at levels 0 and 9, then compare file sizes and verify that decoded pixels match."
version: "1.11.2"
---

# Set PNG compression level in C#

**OneImage** is a free, open-source image library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneImage) · [GitHub](https://github.com/recursive-ai-oss/oneimage-csharp) · [Docs](https://recursiveai.org/oneimage/docs/) · [Download example (.zip)](https://recursiveai.org/oneimage/cookbook/set-png-compression-level/set-png-compression-level.zip)

Save a PNG at compression level 9 without changing its pixels. `checker.png` is in the example ZIP.

```shell
dotnet add package RecursiveAI.OneImage
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OneImage;
var encoder = new PngEncoderOptions
    { CompressionLevel = 9 };
Image.Load("checker.png").Save("level-9.png",
    new() { EncoderOptions = encoder });
Console.WriteLine("Wrote level-9.png");
```

Unzip [the example](https://recursiveai.org/oneimage/cookbook/set-png-compression-level/set-png-compression-level.zip), then `dotnet run`.

```text
Wrote level-9.png
```

Level **0** stores the data without compression (largest), **9** aims for the smallest file, and **6** is the default.

`checker.png` is a generated 64×48 RGB checkerboard of 8×8 squares.

## The full program

```csharp
using OneImage;

Image image = Image.Load("checker.png");
foreach (int level in new[] { 0, 9 })
{
    image.Save($"level-{level}.png", new SaveOptions
    {
        EncoderOptions = new PngEncoderOptions
        {
            CompressionLevel = level
        }
    });
}
Console.WriteLine("Wrote level-0.png and level-9.png");

Rgba32[] original = image.ToArray<Rgba32>();
foreach (int level in new[] { 0, 9 })
{
    string path = $"level-{level}.png";
    Rgba32[] decoded = Image.Load(path).ToArray<Rgba32>();
    long size = new FileInfo(path).Length;
    Console.WriteLine($"{path}: {size} bytes; pixels match: {original.SequenceEqual(decoded)}");
}
```

`dotnet run` prints:

```text
Wrote level-0.png and level-9.png
level-0.png: 518 bytes; pixels match: True
level-9.png: 97 bytes; pixels match: True
```

These sizes are for this checkerboard, not a prediction for another image. The program keeps the other encoder settings fixed and reloads each output to compare its RGBA pixels with the input. A level outside 0–9 throws `ArgumentOutOfRangeException`.

## Variations

**Go back to the default level.** If you don’t need to control the compression level, omit the options. OneImage uses level 6.

```csharp
using OneImage;
Image.Load("checker.png").Save("default.png");
```

## Related recipes

- [Compress an image](https://recursiveai.org/oneimage/cookbook/compress-image/)
- [Save a lossless WebP](https://recursiveai.org/oneimage/cookbook/write-lossless-webp/)

---

## The short answer

Set `PngEncoderOptions.CompressionLevel` to a value from 0 through 9 and pass it in `SaveOptions.EncoderOptions`. The default is 6. To check the effect on your own file, encode it twice with the same settings apart from the level, then compare the output sizes.

## What does PNG compression level mean?

The level controls how the encoder packs the PNG data with [Deflate compression](https://www.w3.org/TR/png-3/#compression). At level 0 the Deflate blocks store the data without compressing it; at level 9 the encoder searches for a more compact representation. Both files use the PNG format and decode to the same pixels in the example. Level 0 is not a dump of raw RGB bytes: the file still includes PNG structure, and OneImage can still use row filters and a palette with its default settings. The sample has just two colors, so its level-0 file is already much smaller than 64×48×3 raw RGB bytes.

## How do you shrink a PNG without losing quality?

Start by raising the compression level without resizing the image or reducing its colors. Level 9 doesn’t promise a particular number of bytes; repeat the comparison on the images you actually need to ship. OneImage’s default palette mode also writes an exact palette for eligible 8-bit RGB or RGBA images with at most 256 distinct colors, counting alpha. That saves repeated color values without discarding them. It is different from forcing an indexed PNG for an 8-bit RGB or RGBA input with more than 256 colors: that can replace colors so they fit. If pixels must stay unchanged, change only the level and leave color reduction out of the pipeline.
