{"record":{"id":"6b51d9a95ace3e09","repo":"d2phap/ImageGlass","slug":"can-t-allocate-memory-in-webppictureimportbgr","errorCode":null,"errorMessage":"Can't allocate memory in WebPPictureImportBGR","messagePattern":"Can't allocate memory in WebPPictureImportBGR","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"v9/Components/ImageGlass.WebP/WebPWrapper.cs","lineNumber":906,"sourceCode":"            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();\n                ptrStats = Marshal.AllocHGlobal(Marshal.SizeOf(stats));\n                Marshal.StructureToPtr(stats, ptrStats, false);\n                wpic.stats = ptrStats;\n            }\n\n            // Memory for WebP output\n            if (dataWebpSize > 2_147_483_591) dataWebpSize = 2_147_483_591;\n            dataWebp = new byte[bmp.Width * bmp.Height * 32];\n            pinnedArrayHandle = GCHandle.Alloc(dataWebp, GCHandleType.Pinned);\n            var initPtr = pinnedArrayHandle.AddrOfPinnedObject();","sourceCodeStart":888,"sourceCodeEnd":924,"githubUrl":"https://github.com/d2phap/ImageGlass/blob/4a3c4feceffc5a8bb5e56ba836509634aaae47a9/v9/Components/ImageGlass.WebP/WebPWrapper.cs#L888-L924","documentation":"Thrown by AdvancedEncode when LibWebp.WebPPictureImportBGR returns != 1 for a Format24bppRgb bitmap. Same root cause as the BGRA variant: the import into a libwebp-owned buffer failed, usually an allocation failure or a bad WebPPicture state.","triggerScenarios":"Advanced encode of a Format24bppRgb bitmap where WebPPictureImportBGR cannot allocate/import — large dimensions, low address space, or a corrupt wpic.","commonSituations":"Encoding very large 24bpp images; 32-bit process memory pressure; concurrent encodes; a failed prior init.","solutions":["Reduce the bitmap dimensions before encoding.","Run as a 64-bit process.","Throttle concurrent encodes to bound peak memory.","Confirm the wpic was successfully initialized via WebPPictureInitInternal immediately before."],"exampleFix":"// before\nvar bytes = webp.EncodeLossy(bigRgb, quality, speed);\n\n// after\nusing var smaller = new Bitmap(bigRgb.Width / 2, bigRgb.Height / 2, PixelFormat.Format24bppRgb);\nusing (var g = Graphics.FromImage(smaller)) g.DrawImage(bigRgb, 0, 0, smaller.Width, smaller.Height);\nvar bytes = webp.EncodeLossy(smaller, quality, speed);","handlingStrategy":"validation","validationCode":"if (Environment.Is64BitProcess == false && bmp.Width * bmp.Height > 75_000_000)\n    bmp = DownscaleTo(bmp, Math.Max(bmp.Width / 2, 1), Math.Max(bmp.Height / 2, 1), PixelFormat.Format24bppRgb);","typeGuard":"static bool LikelyFitsInMemory(Bitmap bmp) =>\n    (long)bmp.Width * bmp.Height * 3 < (Environment.Is64BitProcess ? 1L << 40 : 1_500_000_000L);","tryCatchPattern":"try { var bytes = webp.EncodeLossy(bmp, quality, speed); }\ncatch (Exception ex) when (ex.Message.Contains(\"WebPPictureImportBGR\"))\n{ bmp = DownscaleTo(bmp, bmp.Width / 2, bmp.Height / 2, PixelFormat.Format24bppRgb); /* retry */ }","preventionTips":["Downscale very large 24bpp images before advanced encode.","Use a 64-bit process for big frames.","Bound concurrency to limit peak native allocation."],"tags":["webp","encoding","advanced-api","out-of-memory","bgr-import"],"backgroundTag":null,"analyzedSha":"4a3c4feceffc5a8bb5e56ba836509634aaae47a9","analyzedAt":"2026-08-13T16:58:15.523Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}