d2phap/ImageGlass · error · Exception
Can't allocate memory in WebPPictureImportBGRA
Error message
Can't allocate memory in WebPPictureImportBGRA
What it means
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.
Source
Thrown at v9/Components/ImageGlass.WebP/WebPWrapper.cs:896
if (bmp.Width > WEBP_MAX_DIMENSION || bmp.Height > WEBP_MAX_DIMENSION)
throw new NotSupportedException($"Bitmap's dimension is too large. Max is {WEBP_MAX_DIMENSION}x{WEBP_MAX_DIMENSION} pixels.");
if (bmp.PixelFormat != PixelFormat.Format24bppRgb && bmp.PixelFormat != PixelFormat.Format32bppArgb)
throw new NotSupportedException("Only support Format24bppRgb and Format32bppArgb pixelFormat.");
// Setup the input data, allocating a the bitmap, width and height
bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);
if (LibWebp.WebPPictureInitInternal(ref wpic) != 1)
throw new Exception("Can't initialize WebPPictureInit");
wpic.width = (int)bmp.Width;
wpic.height = (int)bmp.Height;
wpic.use_argb = 1;
if (bmp.PixelFormat == PixelFormat.Format32bppArgb)
{
// Put the bitmap componets in wpic
int result = LibWebp.WebPPictureImportBGRA(ref wpic, bmpData.Scan0, bmpData.Stride);
if (result != 1)
throw new Exception("Can't allocate memory in WebPPictureImportBGRA");
wpic.colorspace = (uint)WEBP_CSP_MODE.MODE_bgrA;
dataWebpSize = bmp.Width * bmp.Height * 32;
dataWebp = new byte[bmp.Width * bmp.Height * 32]; // Memory for WebP output
}
else
{
// Put the bitmap contents in WebPPicture instance
int result = LibWebp.WebPPictureImportBGR(ref wpic, bmpData.Scan0, bmpData.Stride);
if (result != 1)
throw new Exception("Can't allocate memory in WebPPictureImportBGR");
dataWebpSize = bmp.Width * bmp.Height * 24;
}
// Set up statistics of compression
if (info)
{
stats = new WebPAuxStats();View on GitHub (pinned to 4a3c4fecef)
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.
Example fix
// before var bytes = webp.EncodeLossy(bigArgb, quality, speed); // may exhaust memory // after using var smaller = new Bitmap(bigArgb.Width / 2, bigArgb.Height / 2, PixelFormat.Format32bppArgb); using (var g = Graphics.FromImage(smaller)) g.DrawImage(bigArgb, 0, 0, smaller.Width, smaller.Height); var bytes = webp.EncodeLossy(smaller, quality, speed);
Defensive patterns
Strategy: validation
Validate before calling
// Lower the working set for very large 32bpp images before the BGRA import
if (Environment.Is64BitProcess == false && bmp.Width * bmp.Height > 50_000_000)
bmp = DownscaleTo(bmp, Math.Max(bmp.Width / 2, 1), Math.Max(bmp.Height / 2, 1), PixelFormat.Format32bppArgb); Type guard
static bool LikelyFitsInMemory(Bitmap bmp) =>
(long)bmp.Width * bmp.Height * 4 < (Environment.Is64BitProcess ? 1L << 40 : 1_500_000_000L); Try / catch
try { var bytes = webp.EncodeLossy(bmp, quality, speed); }
catch (Exception ex) when (ex.Message.Contains("WebPPictureImportBGRA"))
{ bmp = DownscaleTo(bmp, bmp.Width / 2, bmp.Height / 2, PixelFormat.Format32bppArgb); /* retry */ } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Encoding huge 32bpp images on x86; concurrent encodes exhausting memory; a prior failed init leaving wpic partially initialized.
Related errors
- Can't allocate memory in WebPPictureImportBGR
- Encoding error: {error_code}
- Can't encode WebP
- Bad configuration parameters
- Can't initialize WebPPictureInit
AI-assisted analysis of d2phap/ImageGlass@4a3c4fecef (2026-08-13).
Data as JSON: /api/errors/d1c70073120b2677.
Report an issue: GitHub.