SixLabors/ImageSharp · error · InvalidImageContentException
Marker has bad length .
Error message
Marker {marker} has bad length {length}. What it means
JpegThrowHelper.ThrowBadMarker throws InvalidImageContentException when a JPEG marker segment declares a length that does not match what the decoder requires for that marker type (e.g. an SOFn/SOs/DQT segment whose stated length is impossible). It means the stream structure violates the JPEG (ITU T.81) specification.
Solutions
- Re-obtain the image from a trusted source and validate checksums if available.
- Catch InvalidImageContentException in batch pipelines and skip/quarantine the offending file.
- Re-encode the file with a standard encoder if the original producer is at fault.
- If decoding untrusted input, add the file to a rejection report instead of letting it crash the worker.
Example fix
// before
using var image = Image.LoadAsync(stream);
// after
try { using var image = await Image.LoadAsync(stream); }
catch (InvalidImageContentException ex)
{
quarantine(stream, $"Bad marker: {ex.Message}");
} Defensive patterns
Strategy: try-catch
Try / catch
try
{
using var image = await Image.LoadAsync(stream);
}
catch (InvalidImageContentException ex)
{
quarantine(stream, ex.Message); // bad marker length
} Prevention
- Validate uploads with a quick parse (SOI marker + non-trivial size) before full decode.
- Quarantine files that fail instead of failing whole batch jobs.
- Re-encode images produced by non-standard tools before ingesting them.
When it happens
Trigger: Decoding a JPEG where a marker segment (identified by the marker parameter, e.g. "SOF0", "DQT") has a declared segment length inconsistent with the component count or table size the decoder derives — corrupt or hand-crafted streams, truncation mid-segment.
Common situations: Corrupted downloads/uploads; files processed by buggy third-party encoders; fuzzed or malicious input in image-processing services; byte-level file corruption on disk.
Related errors
- Bad Quantization Table index
- Unknown Quantization Table precision
- Bad sampling factor.
- {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/ecf8ec8b9fa219a2.
Report an issue: GitHub.
Appendix: source
Thrown at src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs:12
// 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.");View on GitHub (pinned to 59ce6af6fc)