{"record":{"id":"d1c70073120b2677","repo":"d2phap/ImageGlass","slug":"can-t-allocate-memory-in-webppictureimportbgra","errorCode":null,"errorMessage":"Can't allocate memory in WebPPictureImportBGRA","messagePattern":"Can't allocate memory in WebPPictureImportBGRA","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"v9/Components/ImageGlass.WebP/WebPWrapper.cs","lineNumber":896,"sourceCode":"            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            if (bmp.PixelFormat != PixelFormat.Format24bppRgb && bmp.PixelFormat != PixelFormat.Format32bppArgb)\n                throw new NotSupportedException(\"Only support Format24bppRgb and Format32bppArgb pixelFormat.\");\n\n            // Setup the input data, allocating a the bitmap, width and height\n            bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);\n            if (LibWebp.WebPPictureInitInternal(ref wpic) != 1)\n                throw new Exception(\"Can't initialize WebPPictureInit\");\n            wpic.width = (int)bmp.Width;\n            wpic.height = (int)bmp.Height;\n            wpic.use_argb = 1;\n\n            if (bmp.PixelFormat == PixelFormat.Format32bppArgb)\n            {\n                // Put the bitmap componets in wpic\n                int result = LibWebp.WebPPictureImportBGRA(ref wpic, bmpData.Scan0, bmpData.Stride);\n                if (result != 1)\n                    throw new Exception(\"Can't allocate memory in WebPPictureImportBGRA\");\n                wpic.colorspace = (uint)WEBP_CSP_MODE.MODE_bgrA;\n                dataWebpSize = bmp.Width * bmp.Height * 32;\n                dataWebp = new byte[bmp.Width * bmp.Height * 32]; // Memory for WebP output\n            }\n            else\n            {\n                // Put the bitmap contents in WebPPicture instance\n                int result = LibWebp.WebPPictureImportBGR(ref wpic, bmpData.Scan0, bmpData.Stride);\n                if (result != 1)\n                    throw new Exception(\"Can't allocate memory in WebPPictureImportBGR\");\n                dataWebpSize = bmp.Width * bmp.Height * 24;\n\n            }\n\n            // Set up statistics of compression\n            if (info)\n            {\n                stats = new WebPAuxStats();","sourceCodeStart":878,"sourceCodeEnd":914,"githubUrl":"https://github.com/d2phap/ImageGlass/blob/4a3c4feceffc5a8bb5e56ba836509634aaae47a9/v9/Components/ImageGlass.WebP/WebPWrapper.cs#L878-L914","documentation":"Thrown by AdvancedEncode when LibWebp.WebPPictureImportBGRA returns != 1 for a Format32bppArgb bitmap. The import copies source pixels into a libwebp-owned ARGB buffer; a 0 return means the import failed, most often due to an allocation failure for very large pictures or a corrupt WebPPicture state.","triggerScenarios":"Advanced encode of a Format32bppArgb bitmap where WebPPictureImportBGRA cannot allocate/import — large dimensions near the memory ceiling, a 32-bit process running out of address space, or a wpic struct left in a bad state.","commonSituations":"Encoding huge 32bpp images on x86; concurrent encodes exhausting memory; a prior failed init leaving wpic partially initialized.","solutions":["Reduce bitmap dimensions before encoding to lower the ARGB buffer requirement.","Run as a 64-bit process to raise the address-space ceiling.","Avoid concurrent advanced encodes that double peak memory.","Ensure WebPPictureInitInternal succeeded (which the wrapper checks just before) so wpic is clean."],"exampleFix":"// before\nvar bytes = webp.EncodeLossy(bigArgb, quality, speed); // may exhaust memory\n\n// after\nusing var smaller = new Bitmap(bigArgb.Width / 2, bigArgb.Height / 2, PixelFormat.Format32bppArgb);\nusing (var g = Graphics.FromImage(smaller)) g.DrawImage(bigArgb, 0, 0, smaller.Width, smaller.Height);\nvar bytes = webp.EncodeLossy(smaller, quality, speed);","handlingStrategy":"validation","validationCode":"// Lower the working set for very large 32bpp images before the BGRA import\nif (Environment.Is64BitProcess == false && bmp.Width * bmp.Height > 50_000_000)\n    bmp = DownscaleTo(bmp, Math.Max(bmp.Width / 2, 1), Math.Max(bmp.Height / 2, 1), PixelFormat.Format32bppArgb);","typeGuard":"static bool LikelyFitsInMemory(Bitmap bmp) =>\n    (long)bmp.Width * bmp.Height * 4 < (Environment.Is64BitProcess ? 1L << 40 : 1_500_000_000L);","tryCatchPattern":"try { var bytes = webp.EncodeLossy(bmp, quality, speed); }\ncatch (Exception ex) when (ex.Message.Contains(\"WebPPictureImportBGRA\"))\n{ bmp = DownscaleTo(bmp, bmp.Width / 2, bmp.Height / 2, PixelFormat.Format32bppArgb); /* retry */ }","preventionTips":["Downscale very large ARGB images before advanced encode.","Run a 64-bit process to lift the address-space ceiling.","Avoid concurrent advanced encodes that multiply peak allocation."],"tags":["webp","encoding","advanced-api","out-of-memory","bgra-import"],"backgroundTag":null,"analyzedSha":"4a3c4feceffc5a8bb5e56ba836509634aaae47a9","analyzedAt":"2026-08-13T16:58:15.523Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}