{"record":{"id":"1d81581c7bda808a","repo":"d2phap/ImageGlass","slug":"bitmap-contains-no-data","errorCode":null,"errorMessage":"Bitmap contains no data.","messagePattern":"Bitmap contains no data\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"v9/Components/ImageGlass.WebP/WebPWrapper.cs","lineNumber":385,"sourceCode":"    /// <param name=\"quality\">Between 0 (lower quality, lowest file size) and 100 (highest quality, higher file size)</param>\n    public void Save(Bitmap bmp, string pathFileName, int quality = 75)\n    {\n        // 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","sourceCodeStart":367,"sourceCodeEnd":403,"githubUrl":"https://github.com/d2phap/ImageGlass/blob/4a3c4feceffc5a8bb5e56ba836509634aaae47a9/v9/Components/ImageGlass.WebP/WebPWrapper.cs#L367-L403","documentation":"Thrown by WebPWrapper.EncodeLossy when bmp.Width == 0 || bmp.Height == 0. The encoder needs a non-zero source bitmap; a zero-dimension bitmap carries no pixels and is rejected up front as ArgumentException (parameter 'bmp'). This fires before the dimension/pixel-format checks, so it is the first validation gate on the encode path.","triggerScenarios":"Passing a freshly-allocated 'new Bitmap(0, 0)' or 'new Bitmap(w, 0)'; a bitmap whose dimensions were set to zero by an upstream crop/resize that clamped to 0; a placeholder/uninitialized bitmap handed to the encoder.","commonSituations":"An image-resize/crop computes a zero target dimension under edge inputs (selection smaller than 1px); loading a corrupt source produced a 0x0 bitmap; default-constructed bitmap passed by mistake.","solutions":["Validate bmp != null && bmp.Width > 0 && bmp.Height > 0 before calling EncodeLossy.","Clamp upstream resize/crop outputs to a minimum of 1x1 (or skip encoding when the result would be empty).","Guard the source-load path so a 0x0 decode is reported as an error earlier, not handed to the encoder.","Catch ArgumentException at the call site and surface 'source image is empty' to the user."],"exampleFix":"// before\nvar raw = new WebPWrapper().EncodeLossy(bmp, quality);\n\n// after: precondition check\nif (bmp is null || bmp.Width == 0 || bmp.Height == 0)\n    throw new ArgumentException(\"Source bitmap must have non-zero dimensions.\", nameof(bmp));\nvar raw = new WebPWrapper().EncodeLossy(bmp, quality);","handlingStrategy":"validation","validationCode":"if (bmp is null || bmp.Width == 0 || bmp.Height == 0)\n    throw new ArgumentException(\"Source bitmap must have non-zero dimensions.\", nameof(bmp));","typeGuard":"static bool IsEncodable(Bitmap bmp) => bmp is not null && bmp.Width > 0 && bmp.Height > 0;","tryCatchPattern":"try { raw = new WebPWrapper().EncodeLossy(bmp, quality); }\ncatch (ArgumentException ex) when (ex.ParamName == nameof(bmp))\n{ /* source bitmap is empty (0x0) */ }","preventionTips":["Validate non-null and non-zero dimensions before encoding.","Clamp upstream resize/crop results to at least 1x1.","Report a 0x0 decode as an error at the load step, not at encode time."],"tags":["webp","encode","validation","bitmap","argumentexception","imageglass","v9"],"backgroundTag":null,"analyzedSha":"4a3c4feceffc5a8bb5e56ba836509634aaae47a9","analyzedAt":"2026-08-13T16:58:15.523Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}