Skip to content

fix: raster outputs throw InvalidOperationException when Skia cannot allocate the image - #510

Merged
guitarrapc merged 1 commit into
mainfrom
null
Nov 26, 2026
Merged

guitarrapc merged 1 commit into
mainfrom
null

Conversation

@guitarrapc

Copy link
Copy Markdown
Owner

Summary

When Skia can't make the image, the raster outputs of the image builders now throw a clear exception. They used to crash with NullReferenceException. The encode error also no longer blames a missing encoder when the real cause is that the image is too large for that format.

Intent

Skia returns null instead of throwing when it can't create an image, and the builders didn't check for that.

  • Surface limit: SKSurface.Create returns null for any surface over 2,147,483,647 bytes, regardless of available memory. WithSize and WithModulePixelSize can both ask for more than that, so every raster output crashed with a NullReferenceException.
  • Encoder limit: SKImage.Encode returns null both when there is no encoder for the format and when the image exceeds the encoder's limits. For example, WebP accepts at most 16,383 px per side. The builder always reported "no encoder", which was misleading.

API example

// Before: NullReferenceException
// After:  InvalidOperationException: An image of 100000x100000 pixels is too large for Skia to allocate:
//         a raster image takes at most 2,147,483,647 bytes. Use a smaller size, or write SVG.
new QRCodeImageBuilder("hello").WithSize(100_000, 100_000).ToByteArray();

// SVG creates no surface, so it is still written at this size
var svg = new QRCodeImageBuilder("hello").WithSize(100_000, 100_000).ToSvgString();

// Before: NotSupportedException: Skia has no encoder for Webp.
// After:  NotSupportedException: Skia could not encode the 16384x64 image as Webp: it has no encoder for the format,
//         or the image is too large for it (WebP takes at most 16,383 pixels a side).
new QRCodeImageBuilder("hello").WithFormat(SKEncodedImageFormat.Webp, 80).WithSize(16_384, 64).ToByteArray();

Changes

  • SymbolImageBuilderBase:
    • A null from SKSurface.Create now throws InvalidOperationException, the same exception as other layouts that can't be drawn.
    • ToBitmap throws the same exception when SKBitmap.FromImage returns null.
    • A null from SKImage.Encode still throws NotSupportedException, but the message now names both possible causes and the image size.
  • XML docs:
    • The raster outputs' InvalidOperationException docs now include "the image is too large for Skia to allocate".
    • Every NotSupportedException doc on the builders and their static helpers now covers "cannot encode an image this large in it".
  • Tests (ImageBuilderCanvasFitTest):
    • Every raster output throws InvalidOperationException for oversized canvases, through both WithSize and WithModulePixelSize, for QR, Micro QR and rMQR. Skia refuses before allocating, so the test uses no memory.
    • SVG output is still written at the same size.
    • WebP succeeds at 16,383 px per side, and at 16,384 px it throws the new message.
  • Spec: qrcode-symbologies.md gets a dated record next to the earlier note on missing encoders.

🤖 Generated with Claude Code

…allocate the image

SKSurface.Create returns null for a surface over 2,147,483,647 bytes, so
WithSize(100000, 100000) or a large module pixel size made every raster
output throw NullReferenceException. The builders now throw
InvalidOperationException with the size, as for other layouts that cannot
be drawn, and ToBitmap does the same when its copy cannot be allocated.

The encode error no longer reports only a missing encoder: Skia also
returns no data for an image past the encoder's limits, such as WebP above
16,383 pixels a side.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@guitarrapc
guitarrapc merged commit 9fa2e25 into main Nov 26, 2026
28 checks passed
@guitarrapc
guitarrapc deleted the null branch November 26, 2026 11:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant