SixLabors/ImageSharp · error · ArgumentOutOfRangeException

Component index must be between 0 and

Error message

Component index must be between 0 and {this.ComponentCount - 1} inclusive.

What it means

PixelComponentInfo.GetComponentPrecision retrieves the bit precision of a component by index; valid indices are 0 through ComponentCount-1. Any other index throws ArgumentOutOfRangeException because precision is packed for exactly the declared component count.

Solutions

  1. Iterate with for (int i = 0; i < info.ComponentCount; i++)
  2. Match the PixelComponentInfo instance to the actual pixel type you are querying
  3. Guard the index before the call if it comes from external input
  4. Catch ArgumentOutOfRangeException when handling arbitrary pixel formats dynamically

Example fix

// before
for (int i = 0; i <= 4; i++) info.GetComponentPrecision(i);
// after
for (int i = 0; i < info.ComponentCount; i++) info.GetComponentPrecision(i);
Defensive patterns

Strategy: validation

Validate before calling

if ((uint)componentIndex < (uint)info.ComponentCount) { int p = info.GetComponentPrecision(componentIndex); }

Type guard

static bool InRange(PixelComponentInfo info, int i) => (uint)i < (uint)info.ComponentCount;

Try / catch

try { int p = info.GetComponentPrecision(i); } catch (ArgumentOutOfRangeException) { p = 0; }

Prevention

When it happens

Trigger: Calling GetComponentPrecision with a negative index or an index >= the ComponentCount the info was created with, e.g. querying component 3 on RGB (3 components) or using a pixel type's component count different from the PixelComponentInfo's.

Common situations: Looping over a hardcoded component count instead of info.ComponentCount; reusing a PixelComponentInfo built for another pixel type; off-by-one loops using <=.

Related errors


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

Appendix: source

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

            }

            sum += p;
        }

        return new PixelComponentInfo(count, bitsPerPixel - sum, precisionData1, precisionData2);
    }

    /// <summary>
    /// Returns the precision of the component in bits at the given index.
    /// </summary>
    /// <param name="componentIndex">The component index.</param>
    /// <returns>The <see cref="int"/>.</returns>
    /// <exception cref="ArgumentOutOfRangeException">The component index cannot exceed the component range.</exception>
    public int GetComponentPrecision(int componentIndex)
    {
        if (componentIndex < 0 || componentIndex >= this.ComponentCount)
        {
            throw new ArgumentOutOfRangeException($"Component index must be between 0 and {this.ComponentCount - 1} inclusive.");
        }

        long selectedPrecisionData = componentIndex < 8 ? this.precisionData1 : this.precisionData2;
        return (int)((selectedPrecisionData >> (8 * (componentIndex & 7))) & 0xFF);
    }

    /// <summary>
    /// Returns the maximum precision in bits of all components.
    /// </summary>
    /// <returns>The <see cref="int"/>.</returns>
    public int GetMaximumComponentPrecision()
    {
        int maxPrecision = 0;
        for (int i = 0; i < this.ComponentCount; i++)
        {
            int componentPrecision = this.GetComponentPrecision(i);
            if (componentPrecision > maxPrecision)
            {

View on GitHub (pinned to 59ce6af6fc)