d2phap/ImageGlass · error · Exception

Can't initialize WebPPictureInit

Error message

Can't initialize WebPPictureInit

What it means

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.

Source

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

        try
        {
            // Validate the configuration
            if (LibWebp.WebPValidateConfig(ref config) != 1)
                throw new Exception("Bad configuration parameters");

            // test bmp
            if (bmp.Width == 0 || bmp.Height == 0)
                throw new ArgumentException("Bitmap contains no data.", nameof(bmp));
            if (bmp.Width > WEBP_MAX_DIMENSION || bmp.Height > WEBP_MAX_DIMENSION)
                throw new NotSupportedException($"Bitmap's dimension is too large. Max is {WEBP_MAX_DIMENSION}x{WEBP_MAX_DIMENSION} pixels.");
            if (bmp.PixelFormat != PixelFormat.Format24bppRgb && bmp.PixelFormat != PixelFormat.Format32bppArgb)
                throw new NotSupportedException("Only support Format24bppRgb and Format32bppArgb pixelFormat.");

            // Setup the input data, allocating a the bitmap, width and height
            bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);
            if (LibWebp.WebPPictureInitInternal(ref wpic) != 1)
                throw new Exception("Can't initialize WebPPictureInit");
            wpic.width = (int)bmp.Width;
            wpic.height = (int)bmp.Height;
            wpic.use_argb = 1;

            if (bmp.PixelFormat == PixelFormat.Format32bppArgb)
            {
                // Put the bitmap componets in wpic
                int result = LibWebp.WebPPictureImportBGRA(ref wpic, bmpData.Scan0, bmpData.Stride);
                if (result != 1)
                    throw new Exception("Can't allocate memory in WebPPictureImportBGRA");
                wpic.colorspace = (uint)WEBP_CSP_MODE.MODE_bgrA;
                dataWebpSize = bmp.Width * bmp.Height * 32;
                dataWebp = new byte[bmp.Width * bmp.Height * 32]; // Memory for WebP output
            }
            else
            {
                // Put the bitmap contents in WebPPicture instance
                int result = LibWebp.WebPPictureImportBGR(ref wpic, bmpData.Scan0, bmpData.Stride);

View on GitHub (pinned to 4a3c4fecef)

Solutions

  1. Pin a libwebp build that matches the WebPPicture layout the wrapper was compiled against.
  2. Rebuild the wrapper (and its struct definitions) against the libwebp you ship.
  3. Use the simple encoder APIs which do not require WebPPictureInitInternal.

Example fix

// before
var bytes = webp.EncodeLossy(bmp, quality, speed);

// after
var bytes = webp.EncodeLossy(bmp, quality); // simple API, no WebPPicture ABI dependency
Defensive patterns

Strategy: try-catch

Validate before calling

var ver = WebPWrapper.GetVersion();
if (Version.Parse(ver).Major < 1) throw new InvalidOperationException("libwebp ABI mismatch; use simple encoder.");

Try / catch

byte[] bytes;
try { bytes = webp.EncodeLossy(bmp, quality, speed); }
catch (Exception ex) when (ex.Message.Contains("WebPPictureInit"))
{ bytes = webp.EncodeLossy(bmp, quality); /* simple API, no WebPPicture */ }

Prevention

When it happens

Trigger: Calling any advanced encode overload where WebPPictureInitInternal(ref wpic) != 1 — typically a struct-layout mismatch or wrong libwebp build.

Common situations: 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.

Related errors


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