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

  1. 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.
  2. Convert the image to an 8-bit or 16-bit grayscale image before saving (Clone + pixel-type conversion).
  3. 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

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


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)