d2phap/ImageGlass · error · Exception

Encoding error: {error_code}

Error message

Encoding error: {error_code}

What it means

Thrown by AdvancedEncode when LibWebp.WebPEncode returns != 1. Unlike the simple encoder, this path surfaces the native error via wpic.error_code, cast to WebPEncodingError. The message appends the symbolic error name (e.g. VP8_ENC_ERROR_OUT_OF_MEMORY, VP8_ENC_ERROR_BITSTREAM_FINISHED), which is the key diagnostic for why encoding failed.

Source

Thrown at v9/Components/ImageGlass.WebP/WebPWrapper.cs:933

                ptrStats = Marshal.AllocHGlobal(Marshal.SizeOf(stats));
                Marshal.StructureToPtr(stats, ptrStats, false);
                wpic.stats = ptrStats;
            }

            // Memory for WebP output
            if (dataWebpSize > 2_147_483_591) dataWebpSize = 2_147_483_591;
            dataWebp = new byte[bmp.Width * bmp.Height * 32];
            pinnedArrayHandle = GCHandle.Alloc(dataWebp, GCHandleType.Pinned);
            var initPtr = pinnedArrayHandle.AddrOfPinnedObject();
            wpic.custom_ptr = initPtr;

            // Set up a byte-writing method (write-to-memory, in this case)
            LibWebp.OnCallback = new LibWebp.WebPMemoryWrite(MyWriter);
            wpic.writer = Marshal.GetFunctionPointerForDelegate(LibWebp.OnCallback);

            // compress the input samples
            if (LibWebp.WebPEncode(ref config, ref wpic) != 1)
                throw new Exception("Encoding error: " + ((WebPEncodingError)wpic.error_code).ToString());

            // Remove OnCallback
            LibWebp.OnCallback = null;

            // Unlock the pixels
            bmp.UnlockBits(bmpData);
            bmpData = null;

            // Copy webpData to rawWebP
            int size = (int)((long)wpic.custom_ptr - (long)initPtr);
            rawWebP = new byte[size];
            Array.Copy(dataWebp, rawWebP, size);

            // Remove compression data
            pinnedArrayHandle.Free();
            dataWebp = null;

            // Show statistics

View on GitHub (pinned to 4a3c4fecef)

Solutions

  1. Read the appended WebPEncodingError name to find the root cause (out-of-memory vs. bitstream vs. configuration).
  2. For out-of-memory: reduce dimensions or run 64-bit.
  3. For bitstream/config errors: simplify config (use the simple API) or update libwebp.
  4. Ensure the output buffer is large enough; the wrapper allocates Width*Height*32 which can overflow Int32 for huge images — downscale first.

Example fix

// before
try { var bytes = webp.EncodeLossy(bmp, quality, speed, info: true); }
catch (Exception ex) { /* generic 'Encoding error: ...' */ }

// after
try { var bytes = webp.EncodeLossy(bmp, quality, speed); }
catch (Exception ex) when (ex.Message.StartsWith("Encoding error:"))
{
    var errName = ex.Message.Substring("Encoding error:".Length).Trim();
    if (errName.Contains("OUT_OF_MEMORY")) { bmp = Downscale(bmp); /* retry */ }
    else throw;
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Reduce risk before encoding: clamp args and bound working set
int s = Math.Clamp(speed, 0, 9); int q = Math.Clamp(quality, 0, 100);
if ((long)bmp.Width * bmp.Height * 32 > int.MaxValue) bmp = DownscaleTo(bmp, 16383, 16383, bmp.PixelFormat);

Try / catch

try { var bytes = webp.EncodeLossy(bmp, quality, speed); }
catch (Exception ex) when (ex.Message.StartsWith("Encoding error:"))
{
    var err = ex.Message.Substring("Encoding error:".Length).Trim();
    if (err.Contains("OUT_OF_MEMORY")) { bmp = DownscaleTo(bmp, bmp.Width / 2, bmp.Height / 2, bmp.PixelFormat); /* retry once */ }
    else throw;
}

Prevention

When it happens

Trigger: Reaching the final LibWebp.WebPEncode call in AdvancedEncode with a config + picture that the native encoder rejects. Triggers include out-of-memory, picture too large, invalid configuration, aborted encoding, or a write-callback failure (the wrapper's MyWriter copies into a pinned managed buffer).

Common situations: The pinned output buffer (bmp.Width*Height*32 bytes) overflowing or being undersized; very large pictures exhausting memory; a corrupt/aborted encode; thread-level encoding on a build without threading; a custom writer returning 0.

Related errors


AI-assisted analysis of d2phap/ImageGlass@4a3c4fecef (2026-08-13). Data as JSON: /api/errors/0bd2a7b3a32e86e4. Report an issue: GitHub.