Skip to content

Imaging — Resize, Convert & Optimize Images

Granit.Imaging provides a fluent image processing pipeline for transformations such as resize, crop, compress, format conversion, and watermarking. Call StripMetadata() in the pipeline to remove GPS coordinates and camera details from uploaded photos (GDPR). The Magick.NET implementation supports JPEG, PNG, WebP, AVIF, GIF, BMP, and TIFF with built-in security hardening (resource limits, format allowlisting, input size validation).

  • DirectoryGranit.Imaging/ Image processing abstractions, fluent pipeline API
    • Granit.Imaging.MagickNet Magick.NET implementation (JPEG/PNG/WebP/AVIF/GIF/BMP/TIFF)
PackageRoleDepends on
Granit.ImagingIImageProcessor, IImagePipeline fluent APIGranit
Granit.Imaging.MagickNetMagickNetImageProcessor (singleton)Granit.Imaging
[DependsOn(typeof(GranitImagingMagickNetModule))]
public class AppModule : GranitModule { }
builder.AddGranitImagingMagickNet();

Customize resource limits via the options callback:

builder.AddGranitImagingMagickNet(options =>
{
options.MaxMemoryBytes = 512 * 1024 * 1024; // 512 MB (default: 256 MB)
options.MaxWidthPixels = 8192; // 8K px (default: 16384)
options.MaxHeightPixels = 8192;
options.MaxInputBytes = 25 * 1024 * 1024; // 25 MB (default: 50 MB)
options.MaxListLength = 16; // GIF frames (default: 32)
});

MagickNetImageProcessor is registered as a singleton — Magick.NET is thread-safe and initializes its native codec once per process.

IImageProcessor.Load() opens an image and returns an IImagePipeline. The pipeline holds native resources and must be disposed after use:

public class PatientPhotoProcessor(IImageProcessor imageProcessor)
{
public async Task<ImageResult> CreateThumbnailAsync(
Stream sourceImage,
CancellationToken cancellationToken)
{
await using IImagePipeline pipeline = imageProcessor.Load(sourceImage);
return await pipeline
.Resize(200, 200, ResizeMode.Crop)
.StripMetadata()
.Compress(quality: 80)
.ConvertTo(ImageFormat.WebP)
.ToResultAsync(cancellationToken);
}
}
MethodDescription
Resize(width, height, mode)Resize with configurable strategy
Crop(rectangle)Crop to rectangular region
Compress(quality)Set output quality (0-100)
ConvertTo(format)Change output format
Watermark(data, position, opacity)Composite watermark overlay
StripMetadata()Remove EXIF, IPTC, XMP metadata (GDPR)
ToResultAsync()Terminal: encode and return ImageResult
SaveToStreamAsync(stream)Terminal: encode and write to stream
ModeBehavior
MaxFit within bounds, preserving aspect ratio (may be smaller than target)
PadFit within bounds with transparent padding to exact dimensions
CropFill exact dimensions by resizing and center-cropping overflow
StretchStretch to exact dimensions (distorts aspect ratio)
MinResize to minimum bounds covering target dimensions entirely
await pipeline.SaveAsWebPAsync(cancellationToken); // ConvertTo(WebP) + ToResultAsync
await pipeline.SaveAsAvifAsync(cancellationToken); // ConvertTo(AVIF) + ToResultAsync
await pipeline.SaveAsJpegAsync(cancellationToken); // ConvertTo(JPEG) + ToResultAsync
await pipeline.SaveAsPngAsync(cancellationToken); // ConvertTo(PNG) + ToResultAsync
FormatReadWriteNotes
JPEGYesYesLossy, no transparency
PNGYesYesLossless, transparency
WebPYesYesModern lossy/lossless, smaller than JPEG
AVIFYesYesNext-gen, best compression ratio
GIFYesYes256 colors, animation support
BMPYesYesUncompressed bitmap
TIFFYesYesLossless, medical imaging

StripMetadata() removes all EXIF, IPTC, and XMP metadata from images. This is critical for GDPR compliance: uploaded photos often contain GPS coordinates, camera serial numbers, timestamps, and other personally identifiable information.

await using IImagePipeline pipeline = imageProcessor.Load(uploadedPhoto);
ImageResult sanitized = await pipeline
.StripMetadata()
.SaveAsWebPAsync(cancellationToken);

A typical pattern: subscribe to BlobValidatedEvent events to post-process images asynchronously after upload validation. The Wolverine handler downloads the validated blob, applies the image pipeline, and re-uploads the result:

public static class BlobValidatedEventHandler
{
public static async Task HandleAsync(
BlobValidatedEvent @event,
IBlobStorage blobStorage,
IImageProcessor imageProcessor,
CancellationToken cancellationToken)
{
if (@event.ContainerName != "patient-photos")
return;
PresignedDownloadUrl download = await blobStorage
.CreateDownloadUrlAsync("patient-photos", @event.BlobId,
cancellationToken: cancellationToken)
.ConfigureAwait(false);
// fetch image from download.Url, process with imageProcessor,
// re-upload thumbnail to "patient-photos-thumbs" container
}
}

This keeps the upload path fast (202 Accepted) while image processing runs in the background. See Blob Storage domain events for the full event list.

Granit.Imaging.MagickNet applies three layers of defense against malicious image uploads:

Before passing any data to the native ImageMagick decoder, Load() validates magic bytes against a safe raster format allowlist (JPEG, PNG, GIF, BMP, TIFF, WebP, AVIF). Dangerous formats that ImageMagick can parse — SVG (SSRF via xlink:href), MSL (file I/O), MVG, PDF, EPS — are rejected before native code executes.

ImageMagick resource limits are configured at startup to prevent decompression bomb attacks (a 42 KB crafted JPEG can inflate to 4+ GB). Default limits:

LimitDefaultDescription
MaxMemoryBytes256 MBPixel cache memory ceiling
MaxWidthPixels16384Maximum image width
MaxHeightPixels16384Maximum image height
MaxListLength32Maximum frames (GIF animation)
MaxInputBytes50 MBPre-decode input size check
  • Resize / Crop: negative or zero dimensions are rejected with ArgumentOutOfRangeException (prevents integer overflow from intuint cast)
  • Compress: quality is validated in the 0–100 range
  • Non-seekable streams: automatically buffered before validation
CategoryKey typesPackage
ModuleGranitImagingModule, GranitImagingMagickNetModule
ProcessorIImageProcessor, IImagePipeline, ImageResultGranit.Imaging
FormatsImageFormat, ImageSizeGranit.Imaging
ModesResizeMode, CropRectangle, WatermarkPositionGranit.Imaging
OptionsImagingMagickNetOptionsGranit.Imaging.MagickNet
ExtensionsAddGranitImagingMagickNet()Granit.Imaging.MagickNet