{"record":{"id":"8c5ead856204c267","repo":"d2phap/ImageGlass","slug":"can-t-config-preset","errorCode":null,"errorMessage":"Can't config preset","messagePattern":"Can't config preset","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"v9/Components/ImageGlass.WebP/WebPWrapper.cs","lineNumber":540,"sourceCode":"            // Free memory\n            if (unmanagedData != IntPtr.Zero)\n                LibWebp.WebPFree(unmanagedData);\n        }\n    }\n\n\n    /// <summary>Lossless encoding image in bitmap (Advanced encoding API)</summary>\n    /// <param name=\"bmp\">Bitmap with the image</param>\n    /// <param name=\"speed\">Between 0 (fastest, lowest compression) and 9 (slower, best compression)</param>\n    /// <returns>Compressed data</returns>\n    public byte[] EncodeLossless(Bitmap bmp, int speed)\n    {\n        //Initialize configuration structure\n        WebPConfig config = new WebPConfig();\n\n        //Set compression parameters\n        if (LibWebp.WebPConfigInit(ref config, WebPPreset.WEBP_PRESET_DEFAULT, (speed + 1) * 10) == 0)\n            throw new Exception(\"Can't config preset\");\n\n        //Old version of DLL does not support info and WebPConfigLosslessPreset\n        if (LibWebp.WebPGetDecoderVersion() > 1082)\n        {\n            if (LibWebp.WebPConfigLosslessPreset(ref config, speed) == 0)\n                throw new Exception(\"Can't configure lossless preset\");\n        }\n        else\n        {\n            config.lossless = 1;\n            config.method = speed;\n            if (config.method > 6)\n                config.method = 6;\n            config.quality = (speed + 1) * 10;\n        }\n        config.pass = speed + 1;\n        config.thread_level = 1;\n        config.alpha_filtering = 2;","sourceCodeStart":522,"sourceCodeEnd":558,"githubUrl":"https://github.com/d2phap/ImageGlass/blob/4a3c4feceffc5a8bb5e56ba836509634aaae47a9/v9/Components/ImageGlass.WebP/WebPWrapper.cs#L522-L558","documentation":"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.","triggerScenarios":"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.","commonSituations":"Bundling an incompatible libwebp.dll; upgrading the wrapper without updating the native binary; running in an architecture-mismatched process.","solutions":["Ensure the libwebp native binary matches the process architecture and the wrapper's expected ABI.","Call WebPWrapper.GetVersion() to confirm the library loads and reports a sensible version.","Fall back to the simple EncodeLossless(bmp) API which does not depend on WebPConfigInit.","Rebuild/redistribute a known-compatible libwebp build."],"exampleFix":"// before\nvar bytes = webp.EncodeLossless(bmp, speed);\n\n// after\nvar bytes = webp.EncodeLossless(bmp); // simple API fallback if advanced init fails","handlingStrategy":"try-catch","validationCode":"if (Version.Parse(WebPWrapper.GetVersion()).Major < 1)\n    throw new InvalidOperationException(\"libwebp too old or mismatched for advanced lossless.\");","typeGuard":null,"tryCatchPattern":"byte[] bytes;\ntry { bytes = webp.EncodeLossless(bmp, speed); }\ncatch (Exception ex) when (ex.Message == \"Can't config preset\")\n{ bytes = webp.EncodeLossless(bmp); /* simple API fallback */ }","preventionTips":["Bundle a known-compatible libwebp build.","Check GetVersion() at startup.","Keep the simple lossless encoder as a fallback."],"tags":["webp","encoding","lossless","native-abi","config-init"],"backgroundTag":null,"analyzedSha":"4a3c4feceffc5a8bb5e56ba836509634aaae47a9","analyzedAt":"2026-08-13T16:58:15.523Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}