{"record":{"id":"e80d44b065904fc8","repo":"d2phap/ImageGlass","slug":"can-t-initialize-webppictureinit","errorCode":null,"errorMessage":"Can't initialize WebPPictureInit","messagePattern":"Can't initialize WebPPictureInit","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"v9/Components/ImageGlass.WebP/WebPWrapper.cs","lineNumber":886,"sourceCode":"\n        try\n        {\n            // Validate the configuration\n            if (LibWebp.WebPValidateConfig(ref config) != 1)\n                throw new Exception(\"Bad configuration parameters\");\n\n            // test bmp\n            if (bmp.Width == 0 || bmp.Height == 0)\n                throw new ArgumentException(\"Bitmap contains no data.\", nameof(bmp));\n            if (bmp.Width > WEBP_MAX_DIMENSION || bmp.Height > WEBP_MAX_DIMENSION)\n                throw new NotSupportedException($\"Bitmap's dimension is too large. Max is {WEBP_MAX_DIMENSION}x{WEBP_MAX_DIMENSION} pixels.\");\n            if (bmp.PixelFormat != PixelFormat.Format24bppRgb && bmp.PixelFormat != PixelFormat.Format32bppArgb)\n                throw new NotSupportedException(\"Only support Format24bppRgb and Format32bppArgb pixelFormat.\");\n\n            // Setup the input data, allocating a the bitmap, width and height\n            bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);\n            if (LibWebp.WebPPictureInitInternal(ref wpic) != 1)\n                throw new Exception(\"Can't initialize WebPPictureInit\");\n            wpic.width = (int)bmp.Width;\n            wpic.height = (int)bmp.Height;\n            wpic.use_argb = 1;\n\n            if (bmp.PixelFormat == PixelFormat.Format32bppArgb)\n            {\n                // Put the bitmap componets in wpic\n                int result = LibWebp.WebPPictureImportBGRA(ref wpic, bmpData.Scan0, bmpData.Stride);\n                if (result != 1)\n                    throw new Exception(\"Can't allocate memory in WebPPictureImportBGRA\");\n                wpic.colorspace = (uint)WEBP_CSP_MODE.MODE_bgrA;\n                dataWebpSize = bmp.Width * bmp.Height * 32;\n                dataWebp = new byte[bmp.Width * bmp.Height * 32]; // Memory for WebP output\n            }\n            else\n            {\n                // Put the bitmap contents in WebPPicture instance\n                int result = LibWebp.WebPPictureImportBGR(ref wpic, bmpData.Scan0, bmpData.Stride);","sourceCodeStart":868,"sourceCodeEnd":904,"githubUrl":"https://github.com/d2phap/ImageGlass/blob/4a3c4feceffc5a8bb5e56ba836509634aaae47a9/v9/Components/ImageGlass.WebP/WebPWrapper.cs#L868-L904","documentation":"Thrown by AdvancedEncode when LibWebp.WebPPictureInitInternal returns != 1. WebPPictureInitInternal initializes the WebPPicture struct against the native library's internal ABI tag; returning 0 signals an ABI mismatch between the C# struct layout and the loaded libwebp, or a struct that was not zeroed before init.","triggerScenarios":"Calling any advanced encode overload where WebPPictureInitInternal(ref wpic) != 1 — typically a struct-layout mismatch or wrong libwebp build.","commonSituations":"Wrapper compiled against one libwebp version but deployed with another; field order/size differences in WebPPicture between libwebp releases; running x86 DLL in x64 process.","solutions":["Pin a libwebp build that matches the WebPPicture layout the wrapper was compiled against.","Rebuild the wrapper (and its struct definitions) against the libwebp you ship.","Use the simple encoder APIs which do not require WebPPictureInitInternal."],"exampleFix":"// before\nvar bytes = webp.EncodeLossy(bmp, quality, speed);\n\n// after\nvar bytes = webp.EncodeLossy(bmp, quality); // simple API, no WebPPicture ABI dependency","handlingStrategy":"try-catch","validationCode":"var ver = WebPWrapper.GetVersion();\nif (Version.Parse(ver).Major < 1) throw new InvalidOperationException(\"libwebp ABI mismatch; use simple encoder.\");","typeGuard":null,"tryCatchPattern":"byte[] bytes;\ntry { bytes = webp.EncodeLossy(bmp, quality, speed); }\ncatch (Exception ex) when (ex.Message.Contains(\"WebPPictureInit\"))\n{ bytes = webp.EncodeLossy(bmp, quality); /* simple API, no WebPPicture */ }","preventionTips":["Ship a libwebp build whose WebPPicture layout matches the wrapper.","Rebuild the wrapper struct definitions if you change libwebp.","Reserve the simple encoder as the safe fallback."],"tags":["webp","encoding","advanced-api","native-abi","struct-layout"],"backgroundTag":null,"analyzedSha":"4a3c4feceffc5a8bb5e56ba836509634aaae47a9","analyzedAt":"2026-08-13T16:58:15.523Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}