{"record":{"id":"25877eb4d3e37c44","repo":"d2phap/ImageGlass","slug":"only-support-format24bpprgb-and-format32bppargb-pi","errorCode":null,"errorMessage":"Only support Format24bppRgb and Format32bppArgb pixelFormat.","messagePattern":"Only support Format24bppRgb and Format32bppArgb pixelFormat\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"v9/Components/ImageGlass.WebP/WebPWrapper.cs","lineNumber":391,"sourceCode":"        // Write webP file\n        File.WriteAllBytes(pathFileName, rawWebP);\n    }\n\n    /// <summary>Lossy encoding bitmap to WebP (Simple encoding API)</summary>\n    /// <param name=\"bmp\">Bitmap with the image</param>\n    /// <param name=\"quality\">Between 0 (lower quality, lowest file size) and 100 (highest quality, higher file size)</param>\n    /// <returns>Compressed data</returns>\n    public byte[] EncodeLossy(Bitmap bmp, int quality = 75)\n    {\n        // test bmp\n        if (bmp.Width == 0 || bmp.Height == 0)\n            throw new ArgumentException(\"Bitmap contains no data.\", nameof(bmp));\n\n        if (bmp.Width > WEBP_MAX_DIMENSION || bmp.Height > WEBP_MAX_DIMENSION)\n            throw new NotSupportedException($\"Bitmap's dimension is too large. Max is {WEBP_MAX_DIMENSION}x{WEBP_MAX_DIMENSION} pixels.\");\n\n        if (bmp.PixelFormat != PixelFormat.Format24bppRgb && bmp.PixelFormat != PixelFormat.Format32bppArgb)\n            throw new NotSupportedException(\"Only support Format24bppRgb and Format32bppArgb pixelFormat.\");\n\n        BitmapData? bmpData = null;\n        var unmanagedData = IntPtr.Zero;\n\n        try\n        {\n            int size;\n\n            // Get bmp data\n            bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);\n\n            // Compress the bmp data\n            if (bmp.PixelFormat == PixelFormat.Format24bppRgb)\n            {\n                size = LibWebp.WebPEncodeBGR(bmpData.Scan0, bmp.Width, bmp.Height, bmpData.Stride, quality, out unmanagedData);\n            }\n            else\n            {","sourceCodeStart":373,"sourceCodeEnd":409,"githubUrl":"https://github.com/d2phap/ImageGlass/blob/4a3c4feceffc5a8bb5e56ba836509634aaae47a9/v9/Components/ImageGlass.WebP/WebPWrapper.cs#L373-L409","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Convert the bitmap to Format32bppArgb (or Format24bppRgb) by cloning into a new Bitmap with that pixel format before encoding.","Draw the source image onto a new 32bpp Bitmap via Graphics.DrawImage to force a compatible layout.","Validate bmp.PixelFormat up front and reject/convert in your pipeline."],"exampleFix":"// before\nvar bytes = webp.EncodeLossy(indexedBmp, 80);\n\n// after\nBitmap encodable;\nif (indexedBmp.PixelFormat != PixelFormat.Format24bppRgb && indexedBmp.PixelFormat != PixelFormat.Format32bppArgb)\n{\n    encodable = new Bitmap(indexedBmp.Width, indexedBmp.Height, PixelFormat.Format32bppArgb);\n    using (var g = Graphics.FromImage(encodable)) g.DrawImage(indexedBmp, 0, 0);\n}\nelse encodable = indexedBmp;\nvar bytes = webp.EncodeLossy(encodable, 80);","handlingStrategy":"validation","validationCode":"static bool IsWebPEncodableFormat(Bitmap bmp) =>\n    bmp.PixelFormat == PixelFormat.Format24bppRgb ||\n    bmp.PixelFormat == PixelFormat.Format32bppArgb;\n\nif (!IsWebPEncodableFormat(bmp)) bmp = ConvertTo32bppArgb(bmp);","typeGuard":"static bool IsWebPSupportedPixelFormat(PixelFormat pf) =>\n    pf == PixelFormat.Format24bppRgb || pf == PixelFormat.Format32bppArgb;","tryCatchPattern":"try { var bytes = webp.EncodeLossy(bmp, quality); }\ncatch (NotSupportedException ex) when (ex.Message.Contains(\"Format24bppRgb\"))\n{ bmp = ConvertTo32bppArgb(bmp); /* retry */ }","preventionTips":["Always normalize incoming bitmaps to Format32bppArgb in your loader.","Never assume a Bitmap loaded from disk is already 24/32bpp unindexed.","Add a pixel-format assertion at the boundary of your image pipeline."],"tags":["webp","encoding","pixel-format","lossy","validation"],"backgroundTag":null,"analyzedSha":"4a3c4feceffc5a8bb5e56ba836509634aaae47a9","analyzedAt":"2026-08-13T16:58:15.523Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}