{"record":{"id":"934c7b43965977ae","repo":"SixLabors/ImageSharp","slug":"quality-factor-must-be-in-1-100-range","errorCode":null,"errorMessage":"Quality factor must be in [1..100] range.","messagePattern":"Quality factor must be in \\[1\\.\\.100\\] range\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Formats/Jpeg/JpegEncoder.cs","lineNumber":39,"sourceCode":"    /// <summary>\n    /// Backing field for <see cref=\"RestartInterval\"/>\n    /// </summary>\n    private int restartInterval;\n\n    /// <summary>\n    /// Gets the quality, that will be used to encode the image. Quality\n    /// index must be between 1 and 100 (compression from max to min).\n    /// Defaults to <value>75</value>.\n    /// </summary>\n    /// <exception cref=\"ArgumentException\">Quality factor must be in [1..100] range.</exception>\n    public int? Quality\n    {\n        get => this.quality;\n        init\n        {\n            if (value is < 1 or > 100)\n            {\n                throw new ArgumentException(\"Quality factor must be in [1..100] range.\");\n            }\n\n            this.quality = value;\n        }\n    }\n\n    /// <summary>\n    /// Gets a value indicating whether progressive encoding is used.\n    /// </summary>\n    public bool Progressive { get; init; }\n\n    /// <summary>\n    /// Gets number of scans per component for progressive encoding.\n    /// Defaults to <value>4</value>.\n    /// </summary>\n    /// <remarks>\n    /// Number of scans must be between 2 and 64.\n    /// There is at least one scan for the DC coefficients and one for the remaining 63 AC coefficients.","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Formats/Jpeg/JpegEncoder.cs#L21-L57","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nvar encoder = new JpegEncoder { Quality = userQuality }; // ArgumentException if 0 or 101\n// after\nvar encoder = new JpegEncoder { Quality = Math.Clamp(userQuality, 1, 100) };","handlingStrategy":"validation","validationCode":"if (quality is < 1 or > 100) throw new ArgumentOutOfRangeException(nameof(quality), \"Quality must be in [1..100].\");","typeGuard":"static bool IsValidJpegQuality(int q) => q is >= 1 and <= 100;","tryCatchPattern":null,"preventionTips":["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"],"tags":["dotnet","jpeg","encoder","argument-out-of-range"],"backgroundTag":"value-out-of-range","analyzedSha":"59ce6af6fc29027cda277ef62d4d1694a8acce91","analyzedAt":"2026-09-13T18:34:59.331Z","contentChangedAt":"2026-09-13T18:34:59.331Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}