{"record":{"id":"f80cd97a4905f468","repo":"d2phap/ImageGlass","slug":"can-t-configure-preset","errorCode":null,"errorMessage":"Can't configure preset","messagePattern":"Can't configure preset","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"v9/Components/ImageGlass.WebP/WebPWrapper.cs","lineNumber":447,"sourceCode":"            if (unmanagedData != IntPtr.Zero)\n                LibWebp.WebPFree(unmanagedData);\n        }\n    }\n\n\n    /// <summary>Lossy encoding bitmap to WebP (Advanced encoding API)</summary>\n    /// <param name=\"bmp\">Bitmap with the image</param>\n    /// <param name=\"quality\">Between 0 (lower quality, lowest file size) and 100 (highest quality, higher file size)</param>\n    /// <param name=\"speed\">Between 0 (fastest, lowest compression) and 9 (slower, best compression)</param>\n    /// <returns>Compressed data</returns>\n    public byte[] EncodeLossy(Bitmap bmp, int quality, int speed, bool info = false)\n    {\n        // Initialize configuration structure\n        var config = new WebPConfig();\n\n        // Set compression parameters\n        if (LibWebp.WebPConfigInit(ref config, WebPPreset.WEBP_PRESET_DEFAULT, 75) == 0)\n            throw new Exception(\"Can't configure preset\");\n\n        // Add additional tuning:\n        config.method = speed;\n        if (config.method > 6) config.method = 6;\n\n        config.quality = quality;\n        config.autofilter = 1;\n        config.pass = speed + 1;\n        config.segments = 4;\n        config.partitions = 3;\n        config.thread_level = 1;\n        config.alpha_quality = quality;\n        config.alpha_filtering = 2;\n        config.use_sharp_yuv = 1;\n\n        if (LibWebp.WebPGetDecoderVersion() > 1082) // Old version does not support preprocessing 4\n        {\n            config.preprocessing = 4;","sourceCodeStart":429,"sourceCodeEnd":465,"githubUrl":"https://github.com/d2phap/ImageGlass/blob/4a3c4feceffc5a8bb5e56ba836509634aaae47a9/v9/Components/ImageGlass.WebP/WebPWrapper.cs#L429-L465","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify the bundled libwebp binary matches the process architecture (x64 DLL for x64 process) and loads without error.","Check WebPWrapper.GetVersion() returns a sane libwebp version before encoding.","Ship the libwebp build that matches the wrapper's expected ABI; rebuild the wrapper against your libwebp if needed.","Fall back to the simple EncodeLossy(bmp, quality) API which does not require WebPConfigInit."],"exampleFix":"// before\nvar bytes = webp.EncodeLossy(bmp, quality, speed);\n\n// after\nif (Version.Parse(WebPWrapper.GetVersion()).Major < 1)\n    throw new InvalidOperationException(\"Incompatible libwebp; use simple encoder.\");\nvar bytes = webp.EncodeLossy(bmp, quality); // simple API, no WebPConfigInit","handlingStrategy":"try-catch","validationCode":"var ver = WebPWrapper.GetVersion();\nif (string.IsNullOrEmpty(ver) || Version.Parse(ver).Major < 1)\n    throw new InvalidOperationException(\"libwebp not loaded or too old for advanced encode.\");","typeGuard":null,"tryCatchPattern":"byte[] bytes;\ntry { bytes = webp.EncodeLossy(bmp, quality, speed); }\ncatch (Exception ex) when (ex.Message == \"Can't configure preset\")\n{ bytes = webp.EncodeLossy(bmp, quality); /* simple API fallback */ }","preventionTips":["Ship a libwebp build that matches the wrapper's ABI and process architecture.","Call GetVersion() at startup to confirm the native library is healthy.","Keep the simple encoder as a documented fallback."],"tags":["webp","encoding","native-abi","config-init","lossy"],"backgroundTag":null,"analyzedSha":"4a3c4feceffc5a8bb5e56ba836509634aaae47a9","analyzedAt":"2026-08-13T16:58:15.523Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}