# Downsample images in a PDF in C#

> Lower the resolution of oversized images in a PDF from .NET with MaxImageDpi, measured where each image is drawn, the result verified. MIT licensed.

Available since 1.1.

Bring every image down to the resolution the page actually shows it at.

## Short answer

Set `MaxImageDpi` on `PdfOptimizerOptions`. The optimizer measures each image at the largest size
it is drawn anywhere in the document, and resamples images drawn above 1.5 times the target down
to the target, with a Lanczos filter. A raw image stays lossless after resampling and a JPEG stays a
JPEG, so nothing changes format unless you also set `ConvertImagesToJpeg`.

```csharp
var options = new PdfOptimizerOptions { MaxImageDpi = 150 };
PdfOptimizationResult result = await PdfOptimizer.OptimizeFileAsync("oversampled.pdf", "downsampled.pdf", options);
Console.WriteLine($"{result.Statistics.ImagesDownsampled} image(s) downsampled, {result.SavingPercent:F1}% saved");
Console.WriteLine(result.Reason);
```

The sample's photograph, 1600 pixels drawn 3 inches wide (about 530 dpi), becomes a 150 dpi image
and the file shrinks by 93.8%.

## Notes

- Masks, bilevel, indexed, 16 bit, Lab, Separation and DeviceN images, JPEG 2000, JBIG2 and CCITT
  images, and images inside Type 3 glyphs keep their resolution.
- Each downsampled image is decoded and compared with an exact resample of the original: same
  components and aspect, mean colors kept, and a minimum SSIM. `MinImageSsim` raises that minimum.
- Command line: `pdfnetstack optimize in.pdf out.pdf --max-dpi 150`.

## Related

- [Compress PDF](https://www.jadev-corp.com/guides/compress-pdf/#lossy-image-options)
- [Reduce PDF size for email](https://www.jadev-corp.com/recipes/reduce-pdf-size/)
- [Command line](https://www.jadev-corp.com/cli/)
- API: [PdfOptimizerOptions](https://www.jadev-corp.com/api/pdfnetstack/pdfoptimizeroptions/), [OptimizationStatistics](https://www.jadev-corp.com/api/pdfnetstack/optimizationstatistics/)

Source: https://www.jadev-corp.com/pdfnetstack/recipes/downsample-pdf-images/
