SixLabors/ImageSharp · error · InvalidImageContentException
Input stream does not have enough bytes to parse declared…
Error message
Input stream does not have enough bytes to parse declared contents of the {marker:X2} marker. What it means
JpegThrowHelper.ThrowNotEnoughBytesForMarker throws InvalidImageContentException when the input stream ends before the full declared contents of a marker segment could be read — the marker promised N bytes but the stream delivered fewer. This is a truncation signature for JPEG input.
Solutions
- Check the byte stream is complete (compare length to Content-Length/expected size) before decoding.
- Re-download or re-copy the file; verify with a checksum.
- Catch InvalidImageContentException and retry once with a fresh, fully-buffered copy of the data.
- Avoid decoding files still being written; wait for write completion first.
Example fix
// before using var image = Image.Load(partialStream); // after using var buffered = new MemoryStream(); partialStream.CopyTo(buffered); // ensure fully readuffered.Position = 0; using var image = Image.Load(buffered);
Defensive patterns
Strategy: try-catch
Validate before calling
// Ensure the stream is fully buffered and its length matches expectations before decoding.
using var buffered = new MemoryStream();
source.CopyTo(buffered);
if (buffered.Length < expectedMinBytes)
throw new IOException("Source JPEG is truncated."); Try / catch
try
{
using var image = Image.Load(stream);
}
catch (InvalidImageContentException ex) when (ex.Message.Contains("does not have enough bytes"))
{
// truncated input: re-fetch or reject
} Prevention
- Compare downloaded length to Content-Length before decoding.
- Never decode files that are still being written; wait for completion signals.
- Fully buffer network streams before handing them to the decoder.
When it happens
Trigger: Image.Load/LoadAsync on a truncated JPEG: download/upload interrupted mid-file, stream closed early, or a seekable stream whose length was misreported so segment reads run past the end.
Common situations: Partial HTTP downloads; interrupted file copies; streaming a JPEG while it is still being written; reading from a truncated database blob or archive entry.
Related errors
- Unexpected end of stream while reading gif application…
- {errorMessage}
- Marker has bad length .
- Bad Quantization Table index
- Unknown Quantization Table precision
AI-assisted analysis of SixLabors/ImageSharp@59ce6af6fc (2026-09-13).
Data as JSON: /api/errors/854d64508b37cdfb.
Report an issue: GitHub.
Appendix: source
Thrown at src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs:14
// 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)