SixLabors/ImageSharp · error · ArgumentOutOfRangeException

Count must match the length of precision array and cannot…

Error message

Count {count} must match the length of precision array and cannot exceed 16.

What it means

PixelComponentInfo.Create packs per-component precision values into two 64-bit words, so it accepts at most 16 components and requires a precision entry for every component. When the precision array is shorter than count or longer than 16, it throws ArgumentOutOfRangeException naming 'count'.

Solutions

  1. Pass exactly one precision value per component and keep the total at 16 or fewer
  2. Ensure the params array length equals count before calling
  3. If more than 16 components are needed, split into multiple pixel structures — the format cannot represent it
  4. Catch ArgumentOutOfRangeException from format-creation code and surface which precision array was malformed

Example fix

// before
PixelComponentInfo.Create(4, 32, 8, 8, 8); // one short
// after
PixelComponentInfo.Create(4, 32, 8, 8, 8, 8);
Defensive patterns

Strategy: validation

Validate before calling

bool ok = precision is { Length: >= 1 and <= 16 } && precision.Length == count;

Type guard

null

Try / catch

try { var info = PixelComponentInfo.Create(count, bitsPerPixel, precision); } catch (ArgumentOutOfRangeException ex) { throw new InvalidOperationException("Precision array must match component count (max 16)", ex); }

Prevention

When it happens

Trigger: Calling PixelComponentInfo.Create(count, bitsPerPixel, precision...) where precision.Length < count (missing entries) or precision.Length > 16 (too many components).

Common situations: Defining a custom pixel format with more than 16 channels; forgetting a precision entry for one component (e.g. passing 3 precisions for RGBA); generating precision arrays programmatically with an off-by-one.

Related errors


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

Appendix: source

Thrown at src/ImageSharp/PixelFormats/PixelComponentInfo.cs:58

    /// <returns>The <see cref="PixelComponentInfo"/>.</returns>
    /// <exception cref="ArgumentOutOfRangeException">The component precision and index cannot exceed the component range.</exception>
    public static PixelComponentInfo Create<TPixel>(int count, params int[] precision)
        where TPixel : unmanaged, IPixel<TPixel>
        => Create(count, Unsafe.SizeOf<TPixel>() * 8, precision);

    /// <summary>
    /// Creates a new <see cref="PixelComponentInfo"/> instance.
    /// </summary>
    /// <param name="count">The number of components within the pixel format.</param>
    /// <param name="bitsPerPixel">The number of bits per pixel.</param>
    /// <param name="precision">The precision in bits of each component.</param>
    /// <returns>The <see cref="PixelComponentInfo"/>.</returns>
    /// <exception cref="ArgumentOutOfRangeException">The component precision and index cannot exceed the component range.</exception>
    public static PixelComponentInfo Create(int count, int bitsPerPixel, params int[] precision)
    {
        if (precision.Length < count || precision.Length > 16)
        {
            throw new ArgumentOutOfRangeException(nameof(count), $"Count {count} must match the length of precision array and cannot exceed 16.");
        }

        long precisionData1 = 0;
        long precisionData2 = 0;
        int sum = 0;
        for (int i = 0; i < precision.Length; i++)
        {
            int p = precision[i];
            if (p is < 0 or > 255)
            {
                throw new ArgumentOutOfRangeException(nameof(precision), $"Precision {precision.Length} must be between 0 and 255.");
            }

            if (i < 8)
            {
                precisionData1 |= ((long)p) << (8 * i);
            }
            else

View on GitHub (pinned to 59ce6af6fc)