SixLabors/ImageSharp · error · ArgumentOutOfRangeException

Precision must be between 0 and 255.

Error message

Precision {precision.Length} must be between 0 and 255.

What it means

Each component precision must fit in one byte (0–255) because PixelComponentInfo packs eight precisions per 64-bit word. Create throws ArgumentOutOfRangeException naming 'precision' when any precision value is negative or greater than 255. (The message interpolates precision.Length rather than the offending value — a known message quirk.)

Solutions

  1. Clamp/validate each precision to 0..255 before calling Create
  2. Ensure you pass per-component precision (e.g. 8,8,8,8 for 32-bit RGBA), not the total bit depth
  3. Catch ArgumentOutOfRangeException and log the precision array for diagnosis

Example fix

// before
PixelComponentInfo.Create(1, 512, 512); // precision > 255
// after
PixelComponentInfo.Create(2, 1024, 512, 512); // per-component precisions within 0..255
Defensive patterns

Strategy: validation

Validate before calling

bool ok = precision.All(p => p is >= 0 and <= 255);

Type guard

null

Try / catch

try { var info = PixelComponentInfo.Create(count, bpp, precision); } catch (ArgumentOutOfRangeException ex) { log.Error(ex, "Precision values must be 0-255"); throw; }

Prevention

When it happens

Trigger: Calling PixelComponentInfo.Create with a precision array containing a negative number or a value above 255, e.g. bits-per-channel-like values such as 512 or -1.

Common situations: Passing bitsPerPixel instead of per-component precision; computing precision from masks or bit counts that exceed a byte; copy-paste errors putting total bits in the array.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


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

Appendix: source

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

    /// <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
            {
                precisionData2 |= ((long)p) << (8 * (i - 8));
            }

            sum += p;
        }

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

    /// <summary>

View on GitHub (pinned to 59ce6af6fc)