d2phap/ImageGlass · error · Exception

Can't config preset

Error message

Can't config preset

What it means

Thrown by EncodeLossless(Bitmap, int) (advanced lossless) when LibWebp.WebPConfigInit returns 0. The advanced lossless path initializes config with preset WEBP_PRESET_DEFAULT and quality (speed+1)*10; a 0 return means the native config initializer failed, typically an ABI/version mismatch.

Source

Thrown at v9/Components/ImageGlass.WebP/WebPWrapper.cs:540

            // Free memory
            if (unmanagedData != IntPtr.Zero)
                LibWebp.WebPFree(unmanagedData);
        }
    }


    /// <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;

View on GitHub (pinned to 4a3c4fecef)

Solutions

  1. Ensure the libwebp native binary matches the process architecture and the wrapper's expected ABI.
  2. Call WebPWrapper.GetVersion() to confirm the library loads and reports a sensible version.
  3. Fall back to the simple EncodeLossless(bmp) API which does not depend on WebPConfigInit.
  4. Rebuild/redistribute a known-compatible libwebp build.

Example fix

// before
var bytes = webp.EncodeLossless(bmp, speed);

// after
var bytes = webp.EncodeLossless(bmp); // simple API fallback if advanced init fails
Defensive patterns

Strategy: try-catch

Validate before calling

if (Version.Parse(WebPWrapper.GetVersion()).Major < 1)
    throw new InvalidOperationException("libwebp too old or mismatched for advanced lossless.");

Try / catch

byte[] bytes;
try { bytes = webp.EncodeLossless(bmp, speed); }
catch (Exception ex) when (ex.Message == "Can't config preset")
{ bytes = webp.EncodeLossless(bmp); /* simple API fallback */ }

Prevention

When it happens

Trigger: Calling EncodeLossless(bmp, speed) where WebPConfigInit(WEBP_PRESET_DEFAULT, (speed+1)*10) returns 0. Same root causes as the lossy 'Can't configure preset': wrong libwebp binary or incompatible ABI.

Common situations: Bundling an incompatible libwebp.dll; upgrading the wrapper without updating the native binary; running in an architecture-mismatched process.

Related errors


AI-assisted analysis of d2phap/ImageGlass@4a3c4fecef (2026-08-13). Data as JSON: /api/errors/8c5ead856204c267. Report an issue: GitHub.