SixLabors/ImageSharp · error · ArgumentException
Quality factor must be in [1..100] range.
Error message
Quality factor must be in [1..100] range.
What it means
JpegEncoder.Quality is an init-only property validated to be between 1 and 100 inclusive; values outside this range throw ArgumentException at construction/object-initializer time, before any encoding happens.
Solutions
- Clamp the value before assigning: Math.Clamp(quality, 1, 100)
- Validate user/config input range before constructing the encoder
- Use ImageEncoderExtensions' quality-parameter overloads which document the 1-100 range
- Fix scale conversion — e.g. some libraries use 0-100 where 0 must become 1
Example fix
// before
var encoder = new JpegEncoder { Quality = userQuality }; // ArgumentException if 0 or 101
// after
var encoder = new JpegEncoder { Quality = Math.Clamp(userQuality, 1, 100) }; Defensive patterns
Strategy: validation
Validate before calling
if (quality is < 1 or > 100) throw new ArgumentOutOfRangeException(nameof(quality), "Quality must be in [1..100].");
Type guard
static bool IsValidJpegQuality(int q) => q is >= 1 and <= 100;
Prevention
- Always Math.Clamp externally supplied quality to [1..100]
- Remember 0 is invalid — lowest quality is 1
- Centralize encoder construction in one factory that validates inputs
When it happens
Trigger: new JpegEncoder { Quality = 0 } or Quality > 100 (or a negative value), typically computed from user input or an unclamped variable; also int values sourced from config or query strings.
Common situations: Mapping a UI slider or HTTP parameter straight into Quality; storing quality as a 0-99 scale from another library and passing it unclamped; off-by-one when '0 = lowest' is assumed.
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
- Progressive scans must be in [2..64] range.
- Restart interval must be in [0..65535] range.
- Could not find any converter for JpegColorSpace
- Invalid ICC profile.
- color
AI-assisted analysis of SixLabors/ImageSharp@59ce6af6fc (2026-09-13).
Data as JSON: /api/errors/934c7b43965977ae.
Report an issue: GitHub.
Appendix: source
Thrown at src/ImageSharp/Formats/Jpeg/JpegEncoder.cs:39
/// <summary>
/// Backing field for <see cref="RestartInterval"/>
/// </summary>
private int restartInterval;
/// <summary>
/// Gets the quality, that will be used to encode the image. Quality
/// index must be between 1 and 100 (compression from max to min).
/// Defaults to <value>75</value>.
/// </summary>
/// <exception cref="ArgumentException">Quality factor must be in [1..100] range.</exception>
public int? Quality
{
get => this.quality;
init
{
if (value is < 1 or > 100)
{
throw new ArgumentException("Quality factor must be in [1..100] range.");
}
this.quality = value;
}
}
/// <summary>
/// Gets a value indicating whether progressive encoding is used.
/// </summary>
public bool Progressive { get; init; }
/// <summary>
/// Gets number of scans per component for progressive encoding.
/// Defaults to <value>4</value>.
/// </summary>
/// <remarks>
/// Number of scans must be between 2 and 64.
/// There is at least one scan for the DC coefficients and one for the remaining 63 AC coefficients.View on GitHub (pinned to 59ce6af6fc)