CL.Imaging
Image decoding, a lazy editing pipeline, EXIF metadata, colour analysis and web helpers — built on SkiaSharp.
CL.Imaging loads images safely (pixel limit from the header, byte limit while reading), edits them through a
chainable pipeline, reads their metadata and analyses them. Every fallible call returns a CodeLogic Result /
Result<T> whose Error.Code is one of ImagingErrors (imaging.*); bad input never throws. It moved out of
CL.Common in 5.0 (see Migrating from CL.Common).
| Package | CodeLogic.Imaging |
| Library class | CL.Imaging.ImagingLibrary (optional) |
| Config file | config.imaging.json |
| Dependencies | SkiaSharp 4.x · SkiaSharp.NativeAssets.Linux.NoDependencies |
Sub-pages:
- Editing — the pipeline: geometry, colour, filters, drawing, watermarks, encoding.
- Metadata — probing, EXIF, orientation, ICC profiles, stripping.
- Analysis — colours and palettes, BlurHash, perceptual hashes, smart crop, comparison.
Install
dotnet add package CodeLogic.Imaging
Nothing has to be loaded: ImageDocument, ImageTools and the analysis classes work on their own. Load the library
only to drive the defaults from configuration:
await Libraries.LoadAsync<CL.Imaging.ImagingLibrary>(); // optional
Loading
Result<ImageDocument> loaded = await ImageDocument.LoadAsync(stream, new ImageLoadOptions { MaxPixels = 40_000_000 }, ct);
using var doc = loaded.Value!;
| Member | Purpose |
|---|---|
Load(Stream) / LoadAsync(Stream, options, ct) |
Read to the end (byte limit), check the header's pixel count, decode. |
Load(ReadOnlySpan<byte>) |
Decode bytes. |
LoadFile(path) / LoadFileAsync(path, options, ct) |
Decode a file (imaging.file_not_found when missing). |
Create(width, height, background) |
A blank image. |
FromSkiaBitmap(bitmap) / ToSkiaBitmap() |
Interop with SkiaSharp (copies). |
GetPixel(x, y), Clone() |
Read one pixel; deep copy. |
Width, Height, DisplayWidth, DisplayHeight |
Stored size and size once upright. |
Orientation, Metadata, IccProfile, ExifData, SourceFormat |
What was loaded. |
Edit() |
Start a pipeline. |
Encode(format, quality) / Encode(ImageEncodeOptions) |
Encode as is. |
ImageLoadOptions.MaxPixels / MaxBytes default to ImagingDefaults.Current (40 MP, 100 MB); 0 disables a limit.
ReadMetadata = false skips reading EXIF and the ICC profile.
Decodable: JPEG, PNG, WebP, GIF (first frame), BMP, ICO, and HEIF/AVIF where the platform's Skia has the codec
(otherwise imaging.unsupported_format).
One-call helpers (ImageTools)
// Upload: limits, upright, scale down into the box, re-encode. EXIF (including GPS) is dropped.
using var upload = ImageTools.ProcessUpload(stream, new UploadOptions { MaxWidth = 1600, MaxHeight = 1600 }).Value!;
// Responsive variants from one decode; widths above the image's own collapse to one full-size variant.
using var variants = ImageTools.GenerateVariants(doc, [320, 640, 1280], ImageFormat.Webp).Value!;
var srcset = variants.ToSrcSet(v => $"/img/{id}-{v.Width}.webp");
using var thumb = ImageTools.Thumbnail(doc, 256, 256, ResizeMode.Fill, smartCrop: true).Value!;
var dataUri = ImageTools.ToDataUri(doc, ImageFormat.Png).Value!;
ProcessUploadAsync, GenerateVariantsAsync and ThumbnailAsync take a stream and a CancellationToken.
ToDataUri(bytes) wraps already-encoded bytes (MIME type from the signature), and FitWithin(width, height, maxWidth, maxHeight) returns the size scaled down into a box.
Encoded output
EncodedImage holds the bytes in an ArrayPool buffer. Dispose it; copy with ToArray() to keep the bytes longer.
| Member | Purpose |
|---|---|
Format, Width, Height, Length, MimeType |
Describe the output. |
Span, Memory |
The bytes (valid until disposed). |
ToArray(), AsStream(), ToBase64(), ToDataUri() |
Get the bytes out. A stream from AsStream() holds its own reference to the buffer: dispose it when done, and it stays valid even after the image is disposed. |
WriteToAsync(stream, ct), SaveAsync(path, ct) |
Write them. |
AsStream() takes its reference atomically, so it cannot revive a buffer that a concurrent final Dispose has already
returned to the pool; it throws ObjectDisposedException instead.
Configuration
config.imaging.json (only read when ImagingLibrary is loaded; applied live on change). Keys are camelCase:
{
"defaultMaxPixels": 40000000,
"defaultMaxBytes": 104857600,
"defaultQuality": 85,
"defaultFormat": "webp",
"stripMetadataByDefault": true
}
| Setting | Default | Meaning |
|---|---|---|
defaultMaxPixels |
40000000 |
Largest image decoded (0 = no limit). |
defaultMaxBytes |
104857600 |
Largest encoded input read (0 = no limit). |
defaultQuality |
85 |
Lossy quality when a call gives none (1–100). |
defaultFormat |
"webp" |
Output format when a call gives none ("jpeg", "png", "webp"). |
stripMetadataByDefault |
true |
Drop EXIF on encode unless a call keeps it. |
Invalid values stop the library at startup; an invalid live change is logged and ignored.
Without the library: ImagingDefaults.Current = ImagingSettings.BuiltIn with { Quality = 90 };.
Errors
| Code | Kind | When |
|---|---|---|
imaging.too_large |
Validation | Over the pixel or byte limit, or beyond the 16384² output cap. |
imaging.unsupported_format |
Validation | HEIF/AVIF without a platform codec; encoding to anything but JPEG/PNG/WebP. |
imaging.decode_failed |
Validation | Not an image, corrupt or truncated. |
imaging.invalid_argument |
Validation | A pipeline argument out of range, a crop outside the image, a bad BlurHash. |
imaging.encode_failed |
Internal | The encoder produced nothing. |
imaging.cancelled |
Unavailable | The CancellationToken was cancelled. |
imaging.file_not_found |
NotFound | A missing input file. |
imaging.io_failed |
Internal | Stream or file I/O failed. |
imaging.processing_failed |
Internal | Unexpected engine failure. |
Result<T> is a union, so a switch can branch on the value and the error kinds:
string reply = ImageInfo.Probe(upload) switch
{
ImageInfo info => $"{info.Width}x{info.Height} {info.MimeType}",
ValidationError e => $"rejected: {e.Code}", // too_large, decode_failed, unsupported_format
Error e => $"failed: {e.Message}",
null => "no result",
};
Limitations
- No animated output: animated GIF/WebP decode their first frame.
- HEIF/AVIF decoding depends on the platform's Skia build; no decoder is bundled.
- Text uses the platform's fonts; minimal Linux containers may have none (use
ImageFont.FromFile).
Migrating from CL.Common
CL.Common.Imaging.CLU_Imaging is gone. ProcessImage → ImageTools.ProcessUpload, ResizeImage →
doc.Edit().Resize(...), CreateThumbnail → ImageTools.Thumbnail, GetImageDimensions / IsValidSize →
ImageInfo.Probe. The full table is in the package's
MIGRATION.md.