SixLabors/ImageSharp · error · InvalidImageContentException

Bad sampling factor

Error message

Bad sampling factor: {factor}

What it means

JpegThrowHelper.ThrowBadSampling(int factor) throws InvalidImageContentException when the JPEG decoder reads a chroma/luma sampling factor from an SOF frame header that is not a valid sampling value (per JPEG spec, sampling factors must be 1-4 and powers of two per component). It signals corrupt, truncated, or non-conformant JPEG data rather than a caller mistake.

Solutions

  1. Treat the input file as corrupt: re-obtain or re-download the image and validate it (e.g. Image.Identify in a probe) before processing.
  2. Catch InvalidImageContentException around load/decode calls and skip or quarantine the offending file.
  3. If the file is yours, re-encode it with a known-good tool (ImageSharp itself, or libjpeg-based converters) to normalize the sampling factors.
Defensive patterns

Strategy: try-catch

Try / catch

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

Prevention

When it happens

Trigger: Decoding a JPEG whose SOF0/SOF2 marker declares a component sampling factor of 0, or > 4, or a non-power-of-two value; passing a corrupted or hand-crafted JPEG byte stream to Image.Load/Image.Identify/ImageExtensions.SaveAsJpeg decode paths.

Common situations: Processing user-uploaded or scraped JPEGs that were corrupted in transit or by a bad resizer; fuzzed/malicious files; files produced by non-conformant encoders or bit flips on storage; concatenating streams so the decoder parses garbage as a frame header.

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/6fc4102ffb82e18b. Report an issue: GitHub.

Appendix: source

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

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)