SixLabors/ImageSharp · error · InvalidImageContentException
Invalid max pixel value.
Error message
Invalid max pixel value.
What it means
Thrown by PbmDecoderCore.ProcessHeader when a PBM/PGM/PPM header declares a max pixel value (MAXVAL) that is 0 or >= 65536. The PNM specification requires MAXVAL to be between 1 and 65535, so any value outside that range means the file is not a decodable Netpbm image. The library throws InvalidImageContentException during header parsing, before any pixels are read.
Solutions
- Regenerate or re-export the image with a conforming PNM writer so MAXVAL is between 1 and 65535.
- Open the file in a text/hex editor and fix the MAXVAL token in the header (e.g. 255 for 8-bit, 65535 for 16-bit).
- Verify the file is actually a Netpbm image (magic P1-P7) and not another format mislabeled with a .pbm/.pgm/.ppm extension.
- Wrap decode in try-catch for InvalidImageContentException and fall back to another decoder if the file is expected to be a different format.
Example fix
// before: decoding a corrupt file directly
using var image = Image.Load("corrupt.pgm");
// after
try
{
using var image = Image.Load("corrupt.pgm");
}
catch (InvalidImageContentException ex)
{
// header MAXVAL out of [1, 65535]; inspect/repair the source file
Console.WriteLine($"Corrupt PNM header: {ex.Message}");
} Defensive patterns
Strategy: validation
Validate before calling
// PNM MAXVAL must be 1..65535; sniff the plain-text header before decoding
using var reader = new StreamReader(stream, leaveOpen: true);
string magic = reader.ReadLine();
if (magic is "P2" or "P3" or "P5" or "P6")
{
// skip comments, read width/height/maxval tokens...
int maxval = ReadNextInt(reader);
if (maxval is < 1 or > 65535)
throw new InvalidDataException($"PNM MAXVAL {maxval} out of range 1..65535");
}
stream.Position = 0; Type guard
static bool IsValidMaxPixelValue(int maxPixelValue) => maxPixelValue > 0 && maxPixelValue < 65536;
Try / catch
try
{
using var image = Image.Load(pnmStream);
}
catch (InvalidImageContentException ex) when (ex.Message == "Invalid max pixel value.")
{
log.LogWarning("Rejected PNM with out-of-range MAXVAL: {Msg}", ex.Message);
} Prevention
- Validate MAXVAL in the 1..65535 range before handing PNM files to the decoder.
- Only write PNM headers with conformant writer code paths; never format MAXVAL manually.
- Scan user-supplied image batches with Image.Identify first and quarantine InvalidImageContentException files.
- Keep the file extension and actual format in sync to avoid feeding foreign formats to the PNM decoder.
When it happens
Trigger: Calling Image.Identify or Image.Decode on a PNM stream whose plain or binary header contains 'MAXVAL' <= 0 or >= 65536 (e.g. a corrupt or hand-written header, or a byte stream misidentified as PBM).
Common situations: Corrupted downloads, truncated or overwritten PNM files, generating PBM files with a buggy custom writer that emits MAXVAL 0, or feeding a non-PNM file to the decoder because the extension was .pgm/.ppm.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- The ANI sequence references a missing frame resource.
- The ANI file does not contain any decodable animation steps.
- The ANI RIFF container size is invalid.
- Reached EOF while reading the header.
- Unknown filter type.
AI-assisted analysis of SixLabors/ImageSharp@59ce6af6fc (2026-09-13).
Data as JSON: /api/errors/ca669cfaf0e0f712.
Report an issue: GitHub.
Appendix: source
Thrown at src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs:158
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();
}
if (this.maxPixelValue <= 0 || this.maxPixelValue >= 65536)
{
throw new InvalidImageContentException("Invalid max pixel value.");
}
if (this.maxPixelValue > 255)
{
this.componentType = PbmComponentType.Short;
}
else
{
this.componentType = PbmComponentType.Byte;
}
stream.SkipWhitespaceAndComments();
}
else
{
this.componentType = PbmComponentType.Bit;
}
View on GitHub (pinned to 59ce6af6fc)