d2phap/ImageGlass · error · NotSupportedException

Only support Format24bppRgb and Format32bppArgb pixelFormat.

Error message

Only support Format24bppRgb and Format32bppArgb pixelFormat.

What it means

Thrown by EncodeLossy(Bitmap, int) when the bitmap's PixelFormat is neither Format24bppRgb nor Format32bppArgb. libwebp's simple BGR/BGRA encoder only ingests those two memory layouts; any other format (8bpp indexed, 16bpp, 48bpp, 64bpp PARGB, etc.) is rejected before the native call.

Source

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

        // Write webP file
        File.WriteAllBytes(pathFileName, rawWebP);
    }

    /// <summary>Lossy encoding bitmap to WebP (Simple encoding API)</summary>
    /// <param name="bmp">Bitmap with the image</param>
    /// <param name="quality">Between 0 (lower quality, lowest file size) and 100 (highest quality, higher file size)</param>
    /// <returns>Compressed data</returns>
    public byte[] EncodeLossy(Bitmap bmp, int quality = 75)
    {
        // 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.");

        BitmapData? bmpData = null;
        var unmanagedData = IntPtr.Zero;

        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
            {

View on GitHub (pinned to 4a3c4fecef)

Solutions

  1. Convert the bitmap to Format32bppArgb (or Format24bppRgb) by cloning into a new Bitmap with that pixel format before encoding.
  2. Draw the source image onto a new 32bpp Bitmap via Graphics.DrawImage to force a compatible layout.
  3. Validate bmp.PixelFormat up front and reject/convert in your pipeline.

Example fix

// before
var bytes = webp.EncodeLossy(indexedBmp, 80);

// after
Bitmap encodable;
if (indexedBmp.PixelFormat != PixelFormat.Format24bppRgb && indexedBmp.PixelFormat != PixelFormat.Format32bppArgb)
{
    encodable = new Bitmap(indexedBmp.Width, indexedBmp.Height, PixelFormat.Format32bppArgb);
    using (var g = Graphics.FromImage(encodable)) g.DrawImage(indexedBmp, 0, 0);
}
else encodable = indexedBmp;
var bytes = webp.EncodeLossy(encodable, 80);
Defensive patterns

Strategy: validation

Validate before calling

static bool IsWebPEncodableFormat(Bitmap bmp) =>
    bmp.PixelFormat == PixelFormat.Format24bppRgb ||
    bmp.PixelFormat == PixelFormat.Format32bppArgb;

if (!IsWebPEncodableFormat(bmp)) bmp = ConvertTo32bppArgb(bmp);

Type guard

static bool IsWebPSupportedPixelFormat(PixelFormat pf) =>
    pf == PixelFormat.Format24bppRgb || pf == PixelFormat.Format32bppArgb;

Try / catch

try { var bytes = webp.EncodeLossy(bmp, quality); }
catch (NotSupportedException ex) when (ex.Message.Contains("Format24bppRgb"))
{ bmp = ConvertTo32bppArgb(bmp); /* retry */ }

Prevention

When it happens

Trigger: Passing an indexed (Format8bppIndexed), Format16bppGray/Rgb, Format48bppRgb, Format64bppPArgb, or Format32bppRgb bitmap into EncodeLossy/Save. Common with GIF/PNG-8 loads, scanned grayscale, or bitmaps created with a non-default PixelFormat.

Common situations: Loading a GIF into a Bitmap (default 8bpp indexed) and trying to encode it as WebP; thumbnail bitmaps created as Format32bppRgb; TIFF frames in unusual formats.

Related errors


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