SixLabors/ImageSharp · error · InvalidImageContentException

Bad Quantization Table index

Error message

Bad Quantization Table index {index}.

What it means

JpegThrowHelper.ThrowBadQuantizationTableIndex throws InvalidImageContentException when a DQT (define quantization table) segment references a table index outside the valid 0..3 range defined by the JPEG specification. The input stream's quantization table identifier is invalid.

Solutions

  1. Treat the file as corrupt: re-export or re-obtain it from the source.
  2. Catch InvalidImageContentException in ingestion pipelines and reject/quarantine the file.
  3. Re-encode the original image with a standards-compliant encoder to produce a valid DQT.

Example fix

// before
using var image = Image.Load(untrustedStream);
// after
try { using var image = Image.Load(untrustedStream); }
catch (InvalidImageContentException)
{
    reject("Invalid JPEG quantization table");
}
Defensive patterns

Strategy: try-catch

Try / catch

try
{
    using var image = Image.Load(stream);
}
catch (InvalidImageContentException ex) when (ex.Message.Contains("Quantization Table index"))
{
    rejectUpload("Malformed JPEG quantization table");
}

Prevention

When it happens

Trigger: Decoding a JPEG whose DQT segment carries a quantization table ID (the parameter shown in the message) greater than 3 or otherwise malformed — corrupt streams or non-conformant encoders.

Common situations: Files produced by broken/embedded-device encoders; bit corruption in transit; fuzzed or malicious uploads to image-processing endpoints.

Related errors


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

Appendix: source

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

// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

namespace SixLabors.ImageSharp.Formats.Jpeg;

internal static class JpegThrowHelper
{
    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)