d2phap/ImageGlass · error · Exception
Can't configure lossless preset
Error message
Can't configure lossless preset
What it means
Thrown by EncodeLossless(Bitmap, int) when LibWebp.WebPConfigLosslessPreset returns 0 for the requested speed level. The wrapper only takes this branch on DLL versions > 1082 (i.e. libwebp that exposes WebPConfigLosslessPreset). Returning 0 means the preset could not be applied, usually because speed is out of the valid 0-9 range or the preset table is unavailable in this build.
Source
Thrown at v9/Components/ImageGlass.WebP/WebPWrapper.cs:546
/// <summary>Lossless encoding image in bitmap (Advanced encoding API)</summary>
/// <param name="bmp">Bitmap with the image</param>
/// <param name="speed">Between 0 (fastest, lowest compression) and 9 (slower, best compression)</param>
/// <returns>Compressed data</returns>
public byte[] EncodeLossless(Bitmap bmp, int speed)
{
//Initialize configuration structure
WebPConfig config = new WebPConfig();
//Set compression parameters
if (LibWebp.WebPConfigInit(ref config, WebPPreset.WEBP_PRESET_DEFAULT, (speed + 1) * 10) == 0)
throw new Exception("Can't config preset");
//Old version of DLL does not support info and WebPConfigLosslessPreset
if (LibWebp.WebPGetDecoderVersion() > 1082)
{
if (LibWebp.WebPConfigLosslessPreset(ref config, speed) == 0)
throw new Exception("Can't configure lossless preset");
}
else
{
config.lossless = 1;
config.method = speed;
if (config.method > 6)
config.method = 6;
config.quality = (speed + 1) * 10;
}
config.pass = speed + 1;
config.thread_level = 1;
config.alpha_filtering = 2;
config.use_sharp_yuv = 1;
config.exact = 0;
return AdvancedEncode(bmp, config, false);
}
View on GitHub (pinned to 4a3c4fecef)
Solutions
- Clamp speed to the documented 0-9 range before calling EncodeLossless(bmp, speed).
- Verify the libwebp build exposes lossless presets (use a stock build rather than a stripped one).
- If the DLL reports <= 1082 the code already falls back to manual config; consider downgrading or pinning a known-good libwebp.
Example fix
// before var bytes = webp.EncodeLossless(bmp, speed); // after int s = Math.Clamp(speed, 0, 9); var bytes = webp.EncodeLossless(bmp, s);
Defensive patterns
Strategy: validation
Validate before calling
int s = Math.Clamp(speed, 0, 9); var bytes = webp.EncodeLossless(bmp, s);
Type guard
static bool IsValidLosslessSpeed(int speed) => speed >= 0 && speed <= 9;
Try / catch
try { var bytes = webp.EncodeLossless(bmp, speed); }
catch (Exception ex) when (ex.Message.Contains("lossless preset"))
{ var bytes = webp.EncodeLossless(bmp, Math.Clamp(speed, 0, 9)); } Prevention
- Always clamp the lossless speed argument to 0-9.
- Use a stock libwebp build that exposes the preset table.
- Treat a preset-init failure as a signal to verify the native build.
When it happens
Trigger: Calling EncodeLossless(bmp, speed) on a DLL version > 1082 with a speed value outside 0-9, or with a libwebp build whose lossless preset support is broken/limited.
Common situations: Passing a negative speed or speed > 9; a custom libwebp build compiled without the lossless preset module; mismatch between the wrapper's assumed API surface and the actual DLL.
Related errors
- Can't config preset
- Bitmap's dimension is too large. Max is {WEBP_MAX_DIMENSION}
- Only support Format24bppRgb and Format32bppArgb pixelFormat.
- Can't configure preset
- This DLL version not support EncodeNearLossless
AI-assisted analysis of d2phap/ImageGlass@4a3c4fecef (2026-08-13).
Data as JSON: /api/errors/b2aeedfe0cf52233.
Report an issue: GitHub.