SixLabors/ImageSharp · error · InvalidImageContentException

Invalid image dimensions

Error message

Invalid image dimensions: {width}x{height}.

What it means

JpegThrowHelper.ThrowInvalidImageDimensions(width, height) throws InvalidImageContentException when the dimensions parsed from a JPEG SOF (frame header) marker are invalid, e.g. zero, negative after ushort decode semantics, or otherwise impossible for the decoder to allocate. It flags corrupt or non-conformant JPEG content, not a caller bug.

Solutions

  1. Verify the input is an intact JPEG (starts with FFD8, plausible SOF) before decoding; re-obtain the file if corrupt.
  2. Catch InvalidImageContentException around load/identify calls and skip or report the file.
  3. If dimensions matter for your pipeline, pre-validate with Image.Identify and check IImageInfo dimensions before full decode.
Defensive patterns

Strategy: try-catch

Try / catch

// try
{
    using Image image = Image.Load(stream);
}
catch (InvalidImageContentException ex)
{
    logger.LogWarning(ex, "Invalid JPEG dimensions, skipping file");
}

Prevention

When it happens

Trigger: Decoding a JPEG whose SOF marker contains width or height of 0; a corrupted/truncated SOF segment; a non-JPEG file whose bytes happen to be parsed until a bogus SOF is found via Image.Load or Image.Identify.

Common situations: Zero-byte-truncated downloads that still parse partially; files damaged by text-mode transfers; deliberately crafted fuzz inputs; mixups where a non-image file is handed to the loader.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


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

Appendix: source

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

    public static void ThrowNotSupportedException(string errorMessage) => throw new NotSupportedException(errorMessage);

    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)