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
- Clamp the value before assigning: Math.Clamp(interval, 0, 65535).
- Validate user/config input at the boundary and reject or clamp out-of-range intervals.
- 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
- Clamp any user/config-derived interval with Math.Clamp(value, 0, 65535) before assigning.
- Remember the interval is in MCUs and is a 16-bit field; don't compute byte-based values into it.
- Default to 0 (no restart markers) unless you specifically need them.
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
- Quality factor must be in [1..100] range.
- Progressive scans must be in [2..64] range.
- maxDegreeOfParallelism
- color
- Bit depth is not supported or not valid.
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)