{"record":{"id":"ff63bb3eab992e74","repo":"d2phap/ImageGlass","slug":"bitmap-s-dimension-is-too-large-max-is-webp-max","errorCode":null,"errorMessage":"Bitmap's dimension is too large. Max is {WEBP_MAX_DIMENSION}x{WEBP_MAX_DIMENSION} pixels.","messagePattern":"Bitmap's dimension is too large\\. Max is (.+?)x(.+?) pixels\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"v9/Components/ImageGlass.WebP/WebPWrapper.cs","lineNumber":388,"sourceCode":"        // Encode in webP format\n        var rawWebP = EncodeLossy(bmp, quality);\n\n        // 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);","sourceCodeStart":370,"sourceCodeEnd":406,"githubUrl":"https://github.com/d2phap/ImageGlass/blob/4a3c4feceffc5a8bb5e56ba836509634aaae47a9/v9/Components/ImageGlass.WebP/WebPWrapper.cs#L370-L406","documentation":"Thrown by EncodeLossy(Bitmap, int) when the source bitmap's width or height exceeds WEBP_MAX_DIMENSION (16383 pixels), the maximum dimension the libwebp encoder accepts. This is a hard limit enforced by the WebP format itself (the bitstream stores dimensions in 14 bits). The check runs before any native call so it fails fast.","triggerScenarios":"Calling EncodeLossy(bmp, quality) or Save(bmp, path, quality) with a Bitmap whose Width or Height is greater than 16383. Most often a very large panorama, a tiled image, or an image loaded at a DPI-scaled resolution.","commonSituations":"Stitching photos into one huge canvas; loading a high-DPI scan; passing a 20000x10000 Bitmap; forgetting that System.Drawing can load bitmaps far larger than WebP can encode.","solutions":["Downscale the bitmap so neither dimension exceeds 16383 before calling EncodeLossy (e.g. clone into a smaller Bitmap).","Use the AdvancedEncode path with use_scaling, or pre-resize with Graphics.DrawImage.","Split oversized images into tiles and encode each separately."],"exampleFix":"// before\nvar bytes = webp.EncodeLossy(hugeBmp, 80);\n\n// after\nconst int Max = 16383;\nint w = hugeBmp.Width, h = hugeBmp.Height;\ndouble scale = Math.Min(1.0, (double)Max / Math.Max(w, h));\nusing var resized = new Bitmap((int)(w * scale), (int)(h * scale), hugeBmp.PixelFormat);\nusing (var g = Graphics.FromImage(resized)) { g.DrawImage(hugeBmp, 0, 0, resized.Width, resized.Height); }\nvar bytes = webp.EncodeLossy(resized, 80);","handlingStrategy":"validation","validationCode":"const int WEBP_MAX_DIMENSION = 16383;\nif (bmp.Width > WEBP_MAX_DIMENSION || bmp.Height > WEBP_MAX_DIMENSION)\n    throw new ArgumentOutOfRangeException(nameof(bmp),\n        $\"Bitmap {bmp.Width}x{bmp.Height} exceeds WebP max {WEBP_MAX_DIMENSION}x{WEBP_MAX_DIMENSION}.\");","typeGuard":"static bool IsWithinWebPDimensions(Bitmap bmp) =>\n    bmp.Width > 0 && bmp.Height > 0 &&\n    bmp.Width <= 16383 && bmp.Height <= 16383;","tryCatchPattern":"try { var bytes = webp.EncodeLossy(bmp, quality); }\ncatch (NotSupportedException ex) when (ex.Message.Contains(\"dimension is too large\"))\n{ /* downscale and retry, or report */ }","preventionTips":["Pre-validate every bitmap's dimensions against 16383 before any WebP encode.","Centralize a 'prepare for WebP' helper that resizes and converts pixel format.","Treat dimension/pixel-format validation as a pipeline stage, not an afterthought."],"tags":["webp","encoding","dimension-limit","lossy","validation"],"backgroundTag":null,"analyzedSha":"4a3c4feceffc5a8bb5e56ba836509634aaae47a9","analyzedAt":"2026-08-13T16:58:15.523Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}