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

  1. Check the byte stream is complete (compare length to Content-Length/expected size) before decoding.
  2. Re-download or re-copy the file; verify with a checksum.
  3. Catch InvalidImageContentException and retry once with a fresh, fully-buffered copy of the data.
  4. 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

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


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)