d2phap/ImageGlass · error · Exception

Bad configuration parameters

Error message

Bad configuration parameters

What it means

Thrown by AdvancedEncode (the shared advanced encoder used by EncodeLossy/EncodeLossless/EncodeNearLossless advanced overloads) when LibWebp.WebPValidateConfig(ref config) != 1. WebPValidateConfig checks that the WebPConfig fields are internally consistent; a non-1 result means the assembled configuration is illegal (e.g. conflicting fields, out-of-range method/pass, or an incompatible lossless/lossy combination).

Source

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

    /// <param name="config">Configuration for encode</param>
    /// <param name="info">True if need encode info.</param>
    /// <returns>Compressed data</returns>
    private byte[] AdvancedEncode(Bitmap bmp, WebPConfig config, bool info)
    {
        byte[]? rawWebP = null;
        byte[]? dataWebp = null;
        var wpic = new WebPPicture();
        BitmapData? bmpData = null;
        var stats = new WebPAuxStats();
        var ptrStats = IntPtr.Zero;
        var pinnedArrayHandle = new GCHandle();
        int dataWebpSize;

        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)

View on GitHub (pinned to 4a3c4fecef)

Solutions

  1. Inspect which advanced overload you called and whether its forced fields are compatible with your libwebp version.
  2. Update libwebp to a version that accepts the wrapper's full config (partitions=3, segments=4, preprocessing=4 require newer builds).
  3. Use the simple APIs (EncodeLossy(bmp, quality) / EncodeLossless(bmp)) that bypass WebPValidateConfig.
  4. Reproduce the config and call WebPValidateConfig yourself to isolate the offending field.

Example fix

// before
var bytes = webp.EncodeLossy(bmp, quality, speed, info: true); // advanced, may produce bad config

// after
var bytes = webp.EncodeLossy(bmp, quality); // simple API, no WebPValidateConfig risk
Defensive patterns

Strategy: try-catch

Validate before calling

// Advanced config is built internally; pre-validate by preferring the simple API
// unless you need advanced tuning. Clamp speed/quality before calling.
int s = Math.Clamp(speed, 0, 9); int q = Math.Clamp(quality, 0, 100);

Try / catch

try { var bytes = webp.EncodeLossy(bmp, quality, speed); }
catch (Exception ex) when (ex.Message == "Bad configuration parameters")
{ var bytes = webp.EncodeLossy(bmp, quality); /* simple API */ }

Prevention

When it happens

Trigger: Reaching AdvancedEncode with a config whose field combination fails WebPValidateConfig — bad method value, partitions/segments out of range, lossless flag mixed with lossy-only options, or near_lossless set without lossless=1.

Common situations: The advanced overloads in this wrapper set many config fields (method, segments, partitions, alpha_filtering, near_lossless, exact) directly; any combination libwebp rejects produces this error before the bitmap is even inspected.

Related errors


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