{"record":{"id":"be82673471805b4b","repo":"SixLabors/ImageSharp","slug":"image-is-too-large-to-encode-at-width-x-height-for-jpeg","errorCode":null,"errorMessage":"Image is too large to encode at {width}x{height} for JPEG format.","messagePattern":"Image is too large to encode at (.+?)x(.+?) for JPEG format\\.","errorType":"exception","errorClass":"ImageFormatException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs","lineNumber":28,"sourceCode":"    public static void ThrowInvalidImageContentException(string errorMessage) => throw new InvalidImageContentException(errorMessage);\n\n    public static void ThrowBadMarker(string marker, int length) => throw new InvalidImageContentException($\"Marker {marker} has bad length {length}.\");\n\n    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.\");\n\n    public static void ThrowBadQuantizationTableIndex(int index) => throw new InvalidImageContentException($\"Bad Quantization Table index {index}.\");\n\n    public static void ThrowBadQuantizationTablePrecision(int precision) => throw new InvalidImageContentException($\"Unknown Quantization Table precision {precision}.\");\n\n    public static void ThrowBadSampling() => throw new InvalidImageContentException(\"Bad sampling factor.\");\n\n    public static void ThrowBadSampling(int factor) => throw new InvalidImageContentException($\"Bad sampling factor: {factor}\");\n\n    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}.\");\n\n    public static void ThrowInvalidImageDimensions(int width, int height) => throw new InvalidImageContentException($\"Invalid image dimensions: {width}x{height}.\");\n\n    public static void ThrowDimensionsTooLarge(int width, int height) => throw new ImageFormatException($\"Image is too large to encode at {width}x{height} for JPEG format.\");\n\n    public static void ThrowNotSupportedComponentCount(int componentCount) => throw new NotSupportedException($\"Images with {componentCount} components are not supported.\");\n\n    public static void ThrowNotSupportedColorSpace() => throw new NotSupportedException(\"Image color space could not be deduced.\");\n}\n","sourceCodeStart":10,"sourceCodeEnd":34,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs#L10-L34","documentation":"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.","triggerScenarios":"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.","commonSituations":"Downscaling pipelines that skip size checks; scientific/medical tiles; users exporting oversized composites to JPEG; automated thumbnailers fed very large source images.","solutions":["Check image.Width and image.Height before encoding and downscale (image.CloneAndApplyGraphicsChanges or Resize) so both dimensions are <= 65535.","Save oversized images in a format without the 16-bit dimension limit (PNG, TIFF, WebP) instead of JPEG.","Catch ImageFormatException around the encode call and fall back to an alternative format or a tiled export."],"exampleFix":"// before\nimage.SaveAsJpeg(stream); // throws for 70000x5000 panoramas\n\n// after\nif (image.Width > 65535 || image.Height > 65535)\n{\n    int scale = Math.Max(image.Width, image.Height) / 65535 + 1;\n    using Image scaled = image.Clone(ctx => ctx.Resize(image.Width / scale, image.Height / scale));\n    scaled.SaveAsJpeg(stream);\n}\nelse\n{\n    image.SaveAsJpeg(stream);\n}","handlingStrategy":"validation","validationCode":"// Before encoding to JPEG:\nif (image.Width > 65535 || image.Height > 65535)\n{\n    // downscale or choose PNG/TIFF instead\n}\nimage.SaveAsJpeg(stream);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always clamp/resize oversized images before JPEG export.","Pick PNG or TIFF as the target format for images approaching 65535 pixels per side.","Enforce a maximum source-image size at ingestion time."],"tags":["jpeg","encoding","image-dimensions","limit-exceeded"],"backgroundTag":"payload-too-large","analyzedSha":"59ce6af6fc29027cda277ef62d4d1694a8acce91","analyzedAt":"2026-09-13T18:34:59.331Z","contentChangedAt":"2026-09-13T18:34:59.331Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}