{"record":{"id":"fc8bc3ff7700d9e8","repo":"d2phap/ImageGlass","slug":"can-t-encode-webp","errorCode":null,"errorMessage":"Can't encode WebP","messagePattern":"Can't encode WebP","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"v9/Components/ImageGlass.WebP/WebPWrapper.cs","lineNumber":413,"sourceCode":"\n        try\n        {\n            int size;\n\n            // Get bmp data\n            bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);\n\n            // Compress the bmp data\n            if (bmp.PixelFormat == PixelFormat.Format24bppRgb)\n            {\n                size = LibWebp.WebPEncodeBGR(bmpData.Scan0, bmp.Width, bmp.Height, bmpData.Stride, quality, out unmanagedData);\n            }\n            else\n            {\n                size = LibWebp.WebPEncodeBGRA(bmpData.Scan0, bmp.Width, bmp.Height, bmpData.Stride, quality, out unmanagedData);\n            }\n\n            if (size == 0) throw new Exception(\"Can't encode WebP\");\n\n            // Copy image compress data to output array\n            var rawWebP = new byte[size];\n            Marshal.Copy(unmanagedData, rawWebP, 0, size);\n\n            return rawWebP;\n        }\n        catch (Exception ex) { throw ex; }\n        finally\n        {\n            // Unlock the pixels\n            if (bmpData != null)\n                bmp.UnlockBits(bmpData);\n\n            // Free memory\n            if (unmanagedData != IntPtr.Zero)\n                LibWebp.WebPFree(unmanagedData);\n        }","sourceCodeStart":395,"sourceCodeEnd":431,"githubUrl":"https://github.com/d2phap/ImageGlass/blob/4a3c4feceffc5a8bb5e56ba836509634aaae47a9/v9/Components/ImageGlass.WebP/WebPWrapper.cs#L395-L431","documentation":"Thrown by EncodeLossy(Bitmap, int) when the native call WebPEncodeBGR/WebPEncodeBGRA returns size 0, meaning libwebp failed to produce any output. The wrapper wraps it as a bare Exception with no error code, so the precise native cause is lost. It usually signals invalid input pointers, an internal allocation failure, or an internal encoder error.","triggerScenarios":"Calling EncodeLossy after the dimension/pixel-format checks pass, but the underlying libwebp simple encoder returns 0. Triggers: corrupted bitmap data, extremely large near-limit dimensions that exhaust memory, or a quality value outside the native library's accepted range passed through (the wrapper does not clamp quality).","commonSituations":"Passing quality < 0 or > 100 (the wrapper forwards it to libwebp); bitmaps at the dimension ceiling; bitmaps whose Stride is negative or mismatched; running on a 32-bit process that runs out of address space for big frames.","solutions":["Clamp the quality argument to the 0-100 range before calling EncodeLossy.","Reduce the bitmap dimensions to give the native allocator headroom.","Switch to the AdvancedEncode API (EncodeLossy(bmp, quality, speed)) which surfaces the WebPEncodingError code for diagnosis.","Verify you are on a 64-bit process if encoding large images."],"exampleFix":"// before\nvar bytes = webp.EncodeLossy(bmp, quality); // quality may be out of range\n\n// after\nint q = Math.Clamp(quality, 0, 100);\nvar bytes = webp.EncodeLossy(bmp, q);\n// or use advanced API to get a real error code:\n// webp.EncodeLossy(bmp, q, 4);","handlingStrategy":"validation","validationCode":"int q = Math.Clamp(quality, 0, 100);\nif (bmp.Width <= 0 || bmp.Height <= 0) throw new ArgumentException(\"Empty bitmap\");\n// then call EncodeLossy(bmp, q)","typeGuard":null,"tryCatchPattern":"try { var bytes = webp.EncodeLossy(bmp, Math.Clamp(quality, 0, 100)); }\ncatch (Exception ex) when (ex.Message == \"Can't encode WebP\")\n{ /* switch to advanced API to read WebPEncodingError, or reduce dimensions */ }","preventionTips":["Always clamp quality to 0-100 before calling the simple encoder.","Prefer the advanced API when you need a real error code.","For large images, encode in a 64-bit process."],"tags":["webp","encoding","native-call","lossy","out-of-memory"],"backgroundTag":null,"analyzedSha":"4a3c4feceffc5a8bb5e56ba836509634aaae47a9","analyzedAt":"2026-08-13T16:58:15.523Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}