SixLabors/ImageSharp · error · ArgumentException
color
Error message
color
What it means
JpegEncoderCore looks up the preconfigured JpegFrameConfig matching the requested JpegEncodingColor in the static FrameConfigs table. If no entry exists for the given color value, it throws ArgumentException — though the message passes the parameter name ('color') rather than a descriptive message, indicating an unexpected/unsupported color mode reached the encoder core.
Solutions
- Set ColorType to a valid JpegEncodingColor member (e.g. JpegEncodingColor.YCbCr) instead of a raw cast or default value.
- If the value comes from configuration/user input, parse it with Enum.TryParse and reject unknown values.
- Update ImageSharp to a version that supports the desired color mode if you are using a newly added JpegEncodingColor on an older package.
Example fix
// before
var encoder = new JpegEncoder { ColorType = (JpegEncodingColor)configValue };
// after
if (!Enum.TryParse(configValue, out JpegEncodingColor colorType))
colorType = JpegEncodingColor.YCbCr;
var encoder = new JpegEncoder { ColorType = colorType }; Defensive patterns
Strategy: validation
Validate before calling
if (!Enum.IsDefined(typeof(JpegEncodingColor), colorType))
colorType = JpegEncodingColor.YCbCr;
var encoder = new JpegEncoder { ColorType = colorType }; Type guard
static bool IsDefinedColor(JpegEncodingColor c) => Enum.IsDefined(typeof(JpegEncodingColor), c) && c != default;
Try / catch
try
{
image.Save(path, encoder);
}
catch (ArgumentException ex) when (ex.Message == nameof(JpegEncodingColor))
{
// invalid ColorType; retry with default
} Prevention
- Never cast raw ints to JpegEncodingColor; parse config strings with Enum.TryParse.
- Avoid default(JpegEncodingColor) (value 0) — it is not a valid encoding color.
- Re-validate persisted encoder options after upgrading ImageSharp versions.
When it happens
Trigger: Calling Save/SaveAsync with a JpegEncoder whose ColorType (JpegEncodingColor) is a value with no corresponding FrameConfig entry — e.g. a default(JpegEncodingColor) of 0 or an invalid cast of an integer/enum from configuration that is not one of the defined encoding colors (YCbCr, Rgb, Luminance, etc.).
Common situations: Binding ColorType from a config string/number without parsing to the enum; using (JpegEncodingColor)someInt casts; library version changes where a color mode was added/removed and serialized options no longer match; generic encoder wrappers passing default enum values.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- Quality factor must be in [1..100] range.
- Progressive scans must be in [2..64] range.
- Restart interval must be in [0..65535] range.
- No encoder was found for extension
- source
AI-assisted analysis of SixLabors/ImageSharp@59ce6af6fc (2026-09-13).
Data as JSON: /api/errors/20c70ffcf956d366.
Report an issue: GitHub.
Appendix: source
Thrown at src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs:868
static int GetQualityForTable(int destIndex, int? encoderQuality, JpegMetadata metadata) => destIndex switch
{
0 => encoderQuality ?? metadata.LuminanceQuality ?? Quantization.DefaultQualityFactor,
1 => encoderQuality ?? metadata.ChrominanceQuality ?? Quantization.DefaultQualityFactor,
_ => encoderQuality ?? metadata.Quality,
};
}
private JpegFrameConfig GetFrameConfig(JpegMetadata metadata)
{
JpegColorType color = this.encoder.ColorType ?? metadata.ColorType;
JpegFrameConfig frameConfig = Array.Find(
FrameConfigs,
cfg => cfg.EncodingColor == color);
if (frameConfig == null)
{
throw new ArgumentException(nameof(color));
}
return frameConfig;
}
}
View on GitHub (pinned to 59ce6af6fc)