SixLabors/ImageSharp · error · ImageFormatException

Image is too large to encode at

Error message

Image is too large to encode at {width}x{height} for JPEG format.

What it means

JpegThrowHelper.ThrowDimensionsTooLarge(width, height) throws ImageFormatException from the JPEG encoder when the image being saved exceeds what the JPEG format can hold: JPEG stores dimensions in 16-bit fields, so any dimension above 65535 pixels cannot be encoded. This is an encoder-side limit error, thrown by SaveAsJpeg-style calls.

Solutions

  1. Check image.Width and image.Height before encoding and downscale (image.CloneAndApplyGraphicsChanges or Resize) so both dimensions are <= 65535.
  2. Save oversized images in a format without the 16-bit dimension limit (PNG, TIFF, WebP) instead of JPEG.
  3. Catch ImageFormatException around the encode call and fall back to an alternative format or a tiled export.

Example fix

// before
image.SaveAsJpeg(stream); // throws for 70000x5000 panoramas

// after
if (image.Width > 65535 || image.Height > 65535)
{
    int scale = Math.Max(image.Width, image.Height) / 65535 + 1;
    using Image scaled = image.Clone(ctx => ctx.Resize(image.Width / scale, image.Height / scale));
    scaled.SaveAsJpeg(stream);
}
else
{
    image.SaveAsJpeg(stream);
}
Defensive patterns

Strategy: validation

Validate before calling

// Before encoding to JPEG:
if (image.Width > 65535 || image.Height > 65535)
{
    // downscale or choose PNG/TIFF instead
}
image.SaveAsJpeg(stream);

Prevention

When it happens

Trigger: Calling image.SaveAsJpeg / Image.EncodeAsJpeg (or saving with JpegEncoder) on an image whose Width or Height exceeds 65535 pixels, e.g. large panoramas, stitched mosaics, or huge renders.

Common situations: Downscaling pipelines that skip size checks; scientific/medical tiles; users exporting oversized composites to JPEG; automated thumbnailers fed very large source images.

Understand the failure class

Background: payload too large / request exceeds maximum size: why libraries cap bytes and how to fix oversize payloads — this error's family across 50 libraries.

Related errors


AI-assisted analysis of SixLabors/ImageSharp@59ce6af6fc (2026-09-13). Data as JSON: /api/errors/be82673471805b4b. Report an issue: GitHub.

Appendix: source

Thrown at src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs:28

    public static void ThrowInvalidImageContentException(string errorMessage) => throw new InvalidImageContentException(errorMessage);

    public static void ThrowBadMarker(string marker, int length) => throw new InvalidImageContentException($"Marker {marker} has bad length {length}.");

    public static void ThrowNotEnoughBytesForMarker(byte marker) => throw new InvalidImageContentException($"Input stream does not have enough bytes to parse declared contents of the {marker:X2} marker.");

    public static void ThrowBadQuantizationTableIndex(int index) => throw new InvalidImageContentException($"Bad Quantization Table index {index}.");

    public static void ThrowBadQuantizationTablePrecision(int precision) => throw new InvalidImageContentException($"Unknown Quantization Table precision {precision}.");

    public static void ThrowBadSampling() => throw new InvalidImageContentException("Bad sampling factor.");

    public static void ThrowBadSampling(int factor) => throw new InvalidImageContentException($"Bad sampling factor: {factor}");

    public static void ThrowBadProgressiveScan(int ss, int se, int ah, int al) => throw new InvalidImageContentException($"Invalid progressive parameters Ss={ss} Se={se} Ah={ah} Al={al}.");

    public static void ThrowInvalidImageDimensions(int width, int height) => throw new InvalidImageContentException($"Invalid image dimensions: {width}x{height}.");

    public static void ThrowDimensionsTooLarge(int width, int height) => throw new ImageFormatException($"Image is too large to encode at {width}x{height} for JPEG format.");

    public static void ThrowNotSupportedComponentCount(int componentCount) => throw new NotSupportedException($"Images with {componentCount} components are not supported.");

    public static void ThrowNotSupportedColorSpace() => throw new NotSupportedException("Image color space could not be deduced.");
}

View on GitHub (pinned to 59ce6af6fc)