d2phap/ImageGlass · error · Exception
Can't encode WebP
Error message
Can't encode WebP
What it means
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.
Source
Thrown at v9/Components/ImageGlass.WebP/WebPWrapper.cs:413
try
{
int size;
// Get bmp data
bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);
// Compress the bmp data
if (bmp.PixelFormat == PixelFormat.Format24bppRgb)
{
size = LibWebp.WebPEncodeBGR(bmpData.Scan0, bmp.Width, bmp.Height, bmpData.Stride, quality, out unmanagedData);
}
else
{
size = LibWebp.WebPEncodeBGRA(bmpData.Scan0, bmp.Width, bmp.Height, bmpData.Stride, quality, out unmanagedData);
}
if (size == 0) throw new Exception("Can't encode WebP");
// Copy image compress data to output array
var rawWebP = new byte[size];
Marshal.Copy(unmanagedData, rawWebP, 0, size);
return rawWebP;
}
catch (Exception ex) { throw ex; }
finally
{
// Unlock the pixels
if (bmpData != null)
bmp.UnlockBits(bmpData);
// Free memory
if (unmanagedData != IntPtr.Zero)
LibWebp.WebPFree(unmanagedData);
}View on GitHub (pinned to 4a3c4fecef)
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.
Example fix
// before var bytes = webp.EncodeLossy(bmp, quality); // quality may be out of range // after int q = Math.Clamp(quality, 0, 100); var bytes = webp.EncodeLossy(bmp, q); // or use advanced API to get a real error code: // webp.EncodeLossy(bmp, q, 4);
Defensive patterns
Strategy: validation
Validate before calling
int q = Math.Clamp(quality, 0, 100);
if (bmp.Width <= 0 || bmp.Height <= 0) throw new ArgumentException("Empty bitmap");
// then call EncodeLossy(bmp, q) Try / catch
try { var bytes = webp.EncodeLossy(bmp, Math.Clamp(quality, 0, 100)); }
catch (Exception ex) when (ex.Message == "Can't encode WebP")
{ /* switch to advanced API to read WebPEncodingError, or reduce dimensions */ } Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- Bitmap's dimension is too large. Max is {WEBP_MAX_DIMENSION}
- Only support Format24bppRgb and Format32bppArgb pixelFormat.
- Can't configure preset
- Can't allocate memory in WebPPictureImportBGRA
- Can't allocate memory in WebPPictureImportBGR
AI-assisted analysis of d2phap/ImageGlass@4a3c4fecef (2026-08-13).
Data as JSON: /api/errors/fc8bc3ff7700d9e8.
Report an issue: GitHub.