SixLabors/ImageSharp · error · NotSupportedException

Invalid filter method. Expected 0. Was

Error message

Invalid filter method. Expected 0. Was '{this.FilterMethod}'.

What it means

PngHeader.Validate throws this NotSupportedException when the IHDR filter method byte is not 0. The PNG specification currently defines only filter method 0 (adaptive five-tap filtering); any other value means an unknown or future variant the library cannot decode.

Solutions

  1. Re-encode the image with a standard PNG encoder (filter method 0).
  2. Check for corruption and re-obtain the file.
  3. Catch NotSupportedException around the decode and fall back to an alternate decoder if one exists for your use case.

Example fix

// before
using var image = Image.Load(input);
// after
try
{
    using var image = Image.Load(input);
}
catch (NotSupportedException)
{
    // non-standard filter method in IHDR; re-export the image with a standard encoder
}
Defensive patterns

Strategy: try-catch

Validate before calling

// filter method must be 0 in any decodable PNG
typeGuard not needed for byte; pre-check:

Type guard

static bool HasStandardFilterMethod(byte filterMethod) => filterMethod == 0;

Try / catch

try
{
    using var image = Image.Load(pngStream);
}
catch (NotSupportedException ex) when (ex.Message.Contains("filter method"))
{
    // non-standard IHDR; fall back to another decoder or re-encode via external tool
}

Prevention

When it happens

Trigger: Decoding a PNG whose IHDR declares FilterMethod != 0 — produced only by non-standard or experimental encoders, or by corruption of the IHDR bytes.

Common situations: Files from proprietary or experimental PNG extensions, deliberate malformed inputs, or memory/disk corruption flipping the filter-method byte.

Related errors


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

Appendix: source

Thrown at src/ImageSharp/Formats/Png/Chunks/PngHeader.cs:103

    /// </summary>
    /// <exception cref="NotSupportedException">
    /// Thrown if the image does pass validation.
    /// </exception>
    public void Validate()
    {
        if (!PngConstants.ColorTypes.TryGetValue(this.ColorType, out byte[] supportedBitDepths))
        {
            throw new NotSupportedException($"Invalid or unsupported color type. Was '{this.ColorType}'.");
        }

        if (supportedBitDepths.AsSpan().IndexOf(this.BitDepth) == -1)
        {
            throw new NotSupportedException($"Invalid or unsupported bit depth. Was '{this.BitDepth}'.");
        }

        if (this.FilterMethod != 0)
        {
            throw new NotSupportedException($"Invalid filter method. Expected 0. Was '{this.FilterMethod}'.");
        }

        // The png specification only defines 'None' and 'Adam7' as interlaced methods.
        if (this.InterlaceMethod is not PngInterlaceMode.None and not PngInterlaceMode.Adam7)
        {
            throw new NotSupportedException($"Invalid interlace method. Expected 'None' or 'Adam7'. Was '{this.InterlaceMethod}'.");
        }
    }

    /// <summary>
    /// Writes the header to the given buffer.
    /// </summary>
    /// <param name="buffer">The buffer to write to.</param>
    public void WriteTo(Span<byte> buffer)
    {
        BinaryPrimitives.WriteInt32BigEndian(buffer[..4], this.Width);
        BinaryPrimitives.WriteInt32BigEndian(buffer.Slice(4, 4), this.Height);

View on GitHub (pinned to 59ce6af6fc)