SixLabors/ImageSharp · error · InvalidImageContentException

Unknown of not implemented image type encountered.

Error message

Unknown of not implemented image type encountered.

What it means

PbmDecoderCore.ProcessHeader throws InvalidImageContentException when the character after the 'P' magic byte does not correspond to a supported Netpbm subtype. Types '1'-'6' are handled (plain/binary PBM, PGM, PPM); '7' (PAM) is explicitly not implemented, and any other character hits the default case and is rejected.

Solutions

  1. Check the second header byte: only '1'-'6' are supported; route 'P7' (PAM) files to a different decoder or convert them to PPM/PGM first.
  2. Catch InvalidImageContentException around Load and report or skip unsupported Netpbm subtypes.
  3. Pre-screen with Image.DetectFormat and reject PAM explicitly before attempting decode.

Example fix

// before
using Image image = Image.Load(stream); // throws for PAM ('P7') files

// after
Span<byte> magic = stackalloc byte[2];
long pos = stream.Position;
stream.ReadExactly(magic);
stream.Position = pos;
if (magic[0] == (byte)'P' && magic[1] >= (byte)'1' && magic[1] <= (byte)'6')
{
    using Image image = Image.Load(stream);
}
Defensive patterns

Strategy: validation

Validate before calling

// Before decoding as PBM:
Span<byte> magic = stackalloc byte[2];
long pos = stream.Position;
stream.ReadExactly(magic);
stream.Position = pos;
bool supported = magic[0] == (byte)'P' && magic[1] >= (byte)'1' && magic[1] <= (byte)'6';
if (!supported)
{
    // route to a PAM-capable decoder or reject
}

Try / catch

// try
{
    using Image image = Image.Load(stream);
}
catch (InvalidImageContentException ex)
{
    logger.LogWarning(ex, "Unsupported Netpbm subtype (e.g. PAM), skipping");
}

Prevention

When it happens

Trigger: Decoding a PAM file ('P7' header) with the PBM decoder; decoding a corrupted or truncated Netpbm file whose type digit was overwritten; feeding a file whose second byte is not a valid subtype digit.

Common situations: Pipelines that lump all Netpbm formats together and encounter PAM (arbitrary-depth/alpha) files; hand-edited headers; partially overwritten or damaged files from storage or transfer.

Related errors


AI-assisted analysis of SixLabors/ImageSharp@59ce6af6fc (2026-09-13). Data as JSON: /api/errors/806d4269cb3b23b5. Report an issue: GitHub.

Appendix: source

Thrown at src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs:137

                // Binary PBM format: 1 component per pixel, 8 pixels per byte.
                this.colorType = PbmColorType.BlackAndWhite;
                this.encoding = PbmEncoding.Binary;
                break;
            case '5':
                // Binary PGM format: 1 components per pixel, in binary integers.
                this.colorType = PbmColorType.Grayscale;
                this.encoding = PbmEncoding.Binary;
                break;
            case '6':
                // Binary PPM format: 3 components per pixel, in binary integers.
                this.colorType = PbmColorType.Rgb;
                this.encoding = PbmEncoding.Binary;
                break;
            case '7':
            // PAM image: sequence of images.
            // Not implemented yet
            default:
                throw new InvalidImageContentException("Unknown of not implemented image type encountered.");
        }

        if (!stream.SkipWhitespaceAndComments() ||
            !stream.ReadDecimal(out int width) ||
            !stream.SkipWhitespaceAndComments() ||
            !stream.ReadDecimal(out int height) ||
            !stream.SkipWhitespaceAndComments())
        {
            ThrowPrematureEof();
        }

        if (this.colorType != PbmColorType.BlackAndWhite)
        {
            if (!stream.ReadDecimal(out this.maxPixelValue))
            {
                ThrowPrematureEof();
            }

View on GitHub (pinned to 59ce6af6fc)