SixLabors/ImageSharp · error · InvalidImageContentException
Empty or not an PPM image.
Error message
Empty or not an PPM image.
What it means
PbmDecoderCore.ProcessHeader throws InvalidImageContentException when the first two bytes of the stream are not the Netpbm magic ('P' followed by a type digit). Fewer than 2 readable bytes or a non-'P' first byte means the stream is empty or is not a PBM/PGM/PPM file at all, so header processing is aborted.
Solutions
- Verify the stream is non-empty and starts with the 'P' magic byte (optionally followed by 1-6/7) before invoking the PBM decoder.
- Use Image.Identify or Image.DetectFormat first and confirm the detected format is PBM before decoding.
- Catch InvalidImageContentException around Load and route the file to the correct decoder or report it as corrupt/misnamed.
Example fix
// before
using Image image = Image.Load(stream); // throws if not Netpbm
// after
IImageFormat format = Image.DetectFormat(stream);
if (format?.Name == "PBM")
{
stream.Position = 0;
using Image image = Image.Load(stream);
} Defensive patterns
Strategy: validation
Validate before calling
// Before decoding as PBM:
if (stream.Length < 2 || stream.ReadByte() != 'P')
{
throw new InvalidDataException("Not a Netpbm file");
}
stream.Position = 0; Try / catch
// try
{
using Image image = Image.Load(stream);
}
catch (InvalidImageContentException ex)
{
logger.LogWarning(ex, "File is empty or not Netpbm, skipping");
} Prevention
- Check the 'P' magic byte before selecting the PBM decoder.
- Never trust file extensions; detect format from content.
- Reject zero-byte files early in upload handling.
When it happens
Trigger: Calling Image.Load/Image.Identify (PbmDecoder) on an empty stream, a zero-byte file, or a file whose first byte is not 'P' (JPEG, PNG, plain text, HTML error pages saved as .pbm).
Common situations: Misconfigured upload handlers storing the wrong extension; failed downloads producing empty files; automated pipelines trusting file extensions instead of magic bytes.
Related errors
- Unknown of not implemented image type encountered.
- Bitmap does not have a valid format.
- Invalid BMP ICC profile.
- Bad sampling factor
- Invalid progressive parameters Ss=
AI-assisted analysis of SixLabors/ImageSharp@59ce6af6fc (2026-09-13).
Data as JSON: /api/errors/6d7c3b045c65280b.
Report an issue: GitHub.
Appendix: source
Thrown at src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs:98
return new ImageInfo(
new Size(this.pixelSize.Width, this.pixelSize.Height),
this.metadata);
}
/// <summary>
/// Processes the ppm header.
/// </summary>
/// <param name="stream">The input stream.</param>
/// <exception cref="InvalidImageContentException">An EOF marker has been read before the image has been decoded.</exception>
[MemberNotNull(nameof(metadata))]
private void ProcessHeader(BufferedReadStream stream)
{
Span<byte> buffer = stackalloc byte[2];
int bytesRead = stream.Read(buffer);
if (bytesRead != 2 || buffer[0] != 'P')
{
throw new InvalidImageContentException("Empty or not an PPM image.");
}
switch ((char)buffer[1])
{
case '1':
// Plain PBM format: 1 component per pixel, boolean value ('0' or '1').
this.colorType = PbmColorType.BlackAndWhite;
this.encoding = PbmEncoding.Plain;
break;
case '2':
// Plain PGM format: 1 component per pixel, in decimal text.
this.colorType = PbmColorType.Grayscale;
this.encoding = PbmEncoding.Plain;
break;
case '3':
// Plain PPM format: 3 components per pixel, in decimal text.
this.colorType = PbmColorType.Rgb;
this.encoding = PbmEncoding.Plain;View on GitHub (pinned to 59ce6af6fc)