SixLabors/ImageSharp · error · ArgumentException

Restart interval must be in [0..65535] range.

Error message

Restart interval must be in [0..65535] range.

What it means

The JpegEncoder.RestartInterval property validates its value in its init accessor: it must be between 0 and 65535 inclusive because restart interval is encoded as a 16-bit field in the JPEG DRI marker. Passing a negative value or anything above 65535 throws ArgumentException immediately when the encoder options object is constructed.

Solutions

  1. Clamp the value before assigning: Math.Clamp(interval, 0, 65535).
  2. Validate user/config input at the boundary and reject or clamp out-of-range intervals.
  3. If you don't need restart markers, leave RestartInterval at its default (0).

Example fix

// before
var encoder = new JpegEncoder { RestartInterval = userInterval };
// after
var encoder = new JpegEncoder { RestartInterval = Math.Clamp(userInterval, 0, 65535) };
Defensive patterns

Strategy: validation

Validate before calling

if (restartInterval is < 0 or > 65535)
    throw new ArgumentOutOfRangeException(nameof(restartInterval), restartInterval, "Must be in [0..65535].");
var encoder = new JpegEncoder { RestartInterval = restartInterval };

Type guard

static bool IsValidRestartInterval(int value) => value is >= 0 and <= 65535;

Prevention

When it happens

Trigger: Setting JpegEncoder.RestartInterval (directly or via JpegEncoder.RestartInterval property initializer, Image load/save options, or configuration binding) to a value < 0 or > 65535, e.g. restartInterval: 100000 or a negative interval read from user configuration.

Common situations: Computing the interval from image width/height without clamping; binding an unvalidated app setting or CLI flag into encoder options; confusing restart interval (in MCUs) with restart marker spacing in bytes.

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/e93557f1d31fb53a. Report an issue: GitHub.

Appendix: source

Thrown at src/ImageSharp/Formats/Jpeg/JpegEncoder.cs:89

        }
    }

    /// <summary>
    /// Gets numbers of MCUs between restart markers.
    /// Defaults to <value>0</value>.
    /// </summary>
    /// <remarks>
    /// Currently supported in progressive encoding only.
    /// </remarks>
    /// <exception cref="ArgumentException">Restart interval must be in [0..65535] range.</exception>
    public int RestartInterval
    {
        get => this.restartInterval;
        init
        {
            if (value is < 0 or > 65535)
            {
                throw new ArgumentException("Restart interval must be in [0..65535] range.");
            }

            this.restartInterval = value;
        }
    }

    /// <summary>
    /// Gets the component encoding mode.
    /// </summary>
    /// <remarks>
    /// Interleaved encoding mode encodes all color components in a single scan.
    /// Non-interleaved encoding mode encodes each color component in a separate scan.
    /// </remarks>
    public bool? Interleaved { get; init; }

    /// <summary>
    /// Gets the jpeg color for encoding.
    /// </summary>

View on GitHub (pinned to 59ce6af6fc)