d2phap/ImageGlass · error · Exception

Can't configure preset

Error message

Can't configure preset

What it means

Thrown by the advanced EncodeLossy(Bitmap, int, int, bool) when LibWebp.WebPConfigInit returns 0. WebPConfigInit initializes a WebPConfig struct with a preset; returning 0 means the ABI/version handshake with the native libwebp failed or the preset is invalid. Effectively: the wrapper cannot initialize the encoder configuration.

Source

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

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


    /// <summary>Lossy encoding bitmap to WebP (Advanced encoding API)</summary>
    /// <param name="bmp">Bitmap with the image</param>
    /// <param name="quality">Between 0 (lower quality, lowest file size) and 100 (highest quality, higher file size)</param>
    /// <param name="speed">Between 0 (fastest, lowest compression) and 9 (slower, best compression)</param>
    /// <returns>Compressed data</returns>
    public byte[] EncodeLossy(Bitmap bmp, int quality, int speed, bool info = false)
    {
        // Initialize configuration structure
        var config = new WebPConfig();

        // Set compression parameters
        if (LibWebp.WebPConfigInit(ref config, WebPPreset.WEBP_PRESET_DEFAULT, 75) == 0)
            throw new Exception("Can't configure preset");

        // Add additional tuning:
        config.method = speed;
        if (config.method > 6) config.method = 6;

        config.quality = quality;
        config.autofilter = 1;
        config.pass = speed + 1;
        config.segments = 4;
        config.partitions = 3;
        config.thread_level = 1;
        config.alpha_quality = quality;
        config.alpha_filtering = 2;
        config.use_sharp_yuv = 1;

        if (LibWebp.WebPGetDecoderVersion() > 1082) // Old version does not support preprocessing 4
        {
            config.preprocessing = 4;

View on GitHub (pinned to 4a3c4fecef)

Solutions

  1. Verify the bundled libwebp binary matches the process architecture (x64 DLL for x64 process) and loads without error.
  2. Check WebPWrapper.GetVersion() returns a sane libwebp version before encoding.
  3. Ship the libwebp build that matches the wrapper's expected ABI; rebuild the wrapper against your libwebp if needed.
  4. Fall back to the simple EncodeLossy(bmp, quality) API which does not require WebPConfigInit.

Example fix

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

// after
if (Version.Parse(WebPWrapper.GetVersion()).Major < 1)
    throw new InvalidOperationException("Incompatible libwebp; use simple encoder.");
var bytes = webp.EncodeLossy(bmp, quality); // simple API, no WebPConfigInit
Defensive patterns

Strategy: try-catch

Validate before calling

var ver = WebPWrapper.GetVersion();
if (string.IsNullOrEmpty(ver) || Version.Parse(ver).Major < 1)
    throw new InvalidOperationException("libwebp not loaded or too old for advanced encode.");

Try / catch

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

Prevention

When it happens

Trigger: Calling the three-argument EncodeLossy(bmp, quality, speed) where WebPConfigInit(WEBP_PRESET_DEFAULT, 75) returns 0. Most often caused by a libwebp DLL whose version/ABI does not match the wrapper's P/Invoke signatures, or a missing/broken native binary.

Common situations: Deploying with the wrong libwebp.dll build (x86 vs x64, debug vs release); a corrupted or truncated DLL; calling encode before the native library is loaded successfully; version skew between the C# wrapper and the bundled libwebp.

Related errors


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