SixLabors/ImageSharp · error · InvalidImageContentException
Bad sampling factor.
Error message
Bad sampling factor.
What it means
JpegThrowHelper.ThrowBadSampling throws InvalidImageContentException when a frame's component sampling factors are invalid — each component's horizontal/vertical sampling factor must be in 1..4 per the JPEG spec. The parameterless overload reports a generic bad sampling factor.
Solutions
- Re-obtain or re-export the image; the file header is non-conformant.
- Catch InvalidImageContentException and skip/quarantine in batch processing.
- If you control the producer, fix the encoder so sampling factors are within 1..4.
Example fix
// before
using var image = Image.Load(input);
// after
try { using var image = Image.Load(input); }
catch (InvalidImageContentException ex)
{
quarantine(ex, inputPath); // bad sampling factors in SOF
} Defensive patterns
Strategy: try-catch
Try / catch
try
{
using var image = Image.Load(stream);
}
catch (InvalidImageContentException ex) when (ex.Message.Contains("sampling"))
{
quarantine(stream, "Bad sampling factors in SOF");
} Prevention
- Check image headers (SOF sampling factors within 1..4) if you control the producing encoder.
- Reject or quarantine files from known-broken producers instead of decoding blind.
- Validate magic bytes and run a parse check on all untrusted uploads before processing.
When it happens
Trigger: Decoding a JPEG whose SOF0/SOF2 segment declares a component sampling factor of 0 or greater than 4 (the message shows the offending factor in the int overload) — corrupt headers or non-conformant encoder output.
Common situations: Files from broken camera/embedded encoders; bit corruption; fuzzed or malicious uploads; truncated header bytes shifting parsing.
Related errors
- Marker has bad length .
- Bad Quantization Table index
- Unknown Quantization Table precision
- {errorMessage}
- Input stream does not have enough bytes to parse declared…
AI-assisted analysis of SixLabors/ImageSharp@59ce6af6fc (2026-09-13).
Data as JSON: /api/errors/9dcd692b5a3d05c3.
Report an issue: GitHub.
Appendix: source
Thrown at src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs:20
// 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)