SixLabors/ImageSharp · error · ImageFormatException
Component type not supported for Grayscale PBM.
Error message
Component type not supported for Grayscale PBM.
What it means
BinaryEncoder.WritePixels throws ImageFormatException when saving a grayscale PBM whose pixel buffer's component type is neither Byte nor Short. The PBM binary encoder only has fast paths for 8-bit and 16-bit grayscale samples, so any other component type reaching the grayscale branch is rejected as an encoder bug or unsupported pixel type.
Solutions
- Ensure the image's pixel type matches the intended PBM flavor: use L8/L16-style grayscale pixels for grayscale PBM, or set PbmEncoder metadata color type correctly.
- Convert the image to an 8-bit or 16-bit grayscale image before saving (Clone + pixel-type conversion).
- Catch ImageFormatException around SaveAsPbm and fall back to a conversion step or a different format.
Example fix
// before using Image<Rgba32> image = Image.Load<Rgba32>(path); image.SaveAsPbm(stream); // component type not encodable for grayscale PBM // after using Image<L8> gray = Image.Load<L8>(path); gray.SaveAsPbm(stream); // L8 maps to the Byte grayscale path
Defensive patterns
Strategy: validation
Validate before calling
// Only L8/L16-style grayscale pixels map to PBM Byte/Short components.
bool isGrayscalePbmSafe = typeof(TPixel) == typeof(L8) || typeof(TPixel) == typeof(L16);
if (!isGrayscalePbmSafe)
{
// convert to L8 before calling SaveAsPbm
} Prevention
- Use L8/L16 pixel types when saving grayscale PBM.
- Do not hand-edit PBM encoder metadata; let the pixel type drive the component type.
- Test encode paths against every pixel type your pipeline can produce.
When it happens
Trigger: Saving an Image<TPixel> as PBM with PbmColorType.Grayscale while the underlying pixel type maps to a component type other than Byte/Short (e.g. a bit/bitmap-typed buffer misrouted to the grayscale path, or a float-based pixel type routed incorrectly by metadata).
Common situations: Custom encoder plumbing or forks that set PBM metadata manually; pipelines mixing bitonal and grayscale images and mislabeling the color type; encoder regressions after upgrading ImageSharp versions.
Related errors
- Component type not supported for Color PBM.
- No encoder was found for extension
- Image is too large to encode in EXR format.
- Image is too large to encode at
- Empty or not an PPM image.
AI-assisted analysis of SixLabors/ImageSharp@59ce6af6fc (2026-09-13).
Data as JSON: /api/errors/e1f5378e52257b9f.
Report an issue: GitHub.
Appendix: source
Thrown at src/ImageSharp/Formats/Pbm/BinaryEncoder.cs:51
ImageFrame<TPixel> image,
PbmColorType colorType,
PbmComponentType componentType,
CancellationToken cancellationToken)
where TPixel : unmanaged, IPixel<TPixel>
{
if (colorType == PbmColorType.Grayscale)
{
if (componentType == PbmComponentType.Byte)
{
WriteGrayscale(configuration, stream, image, cancellationToken);
}
else if (componentType == PbmComponentType.Short)
{
WriteWideGrayscale(configuration, stream, image, cancellationToken);
}
else
{
throw new ImageFormatException("Component type not supported for Grayscale PBM.");
}
}
else if (colorType == PbmColorType.Rgb)
{
if (componentType == PbmComponentType.Byte)
{
WriteRgb(configuration, stream, image, cancellationToken);
}
else if (componentType == PbmComponentType.Short)
{
WriteWideRgb(configuration, stream, image, cancellationToken);
}
else
{
throw new ImageFormatException("Component type not supported for Color PBM.");
}
}
else if (componentType == PbmComponentType.Bit)View on GitHub (pinned to 59ce6af6fc)