{"record":{"id":"5d35feeea98ff4cb","repo":"dotnet/wpf","slug":"sr-image-insufficientbuffersize-writeablebitmap","errorCode":null,"errorMessage":"SR.Image_InsufficientBufferSize","messagePattern":"SR\\.Image_InsufficientBufferSize","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/WriteableBitmap.cs","lineNumber":860,"sourceCode":"\n                // Nothing to do.\n                return;\n            }\n\n            checked\n            {\n                uint finalRowWidthInBits = (uint)((sourceRect.X + sourceRect.Width) * _format.InternalBitsPerPixel);\n                uint finalRowWidthInBytes = ((finalRowWidthInBits + 7) / 8);\n                uint requiredBufferSize = ((uint)(sourceRect.Y + sourceRect.Height - 1) * (uint)sourceBufferStride) + finalRowWidthInBytes;\n                if (sourceBufferSize < requiredBufferSize)\n                {\n                    if (backwardsCompat)\n                    {\n                        HRESULT.Check((int)WinCodecErrors.WINCODEC_ERR_INSUFFICIENTBUFFER);\n                    }\n                    else\n                    {\n                        throw new ArgumentException(SR.Image_InsufficientBufferSize, nameof(sourceBufferSize));\n                    }\n                }\n\n                uint copyWidthInBits = (uint)(sourceRect.Width * _format.InternalBitsPerPixel);\n\n                // Calculate some offsets that we'll need in a moment.\n                uint sourceXbyteOffset = (uint)((sourceRect.X * _format.InternalBitsPerPixel) / 8);\n                uint sourceBufferBitOffset = (uint)((sourceRect.X * _format.InternalBitsPerPixel) % 8);\n                uint firstPixelByteOffet = (uint)((sourceRect.Y * sourceBufferStride) + sourceXbyteOffset);\n                uint destXbyteOffset = (uint)((destinationX * _format.InternalBitsPerPixel) / 8);\n                uint destBufferBitOffset = (uint)((destinationX * _format.InternalBitsPerPixel) % 8);\n\n                Int32Rect destinationRect = sourceRect;\n                destinationRect.X = destinationX;\n                destinationRect.Y = destinationY;\n\n                //\n                // Copy pixel information from the user supplied buffer to the back buffer.","sourceCodeStart":842,"sourceCodeEnd":878,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/WriteableBitmap.cs#L842-L878","documentation":"After computing the required buffer size (sourceRect area * bytes per pixel), WritePixelsImpl finds the caller-supplied sourceBufferSize too small. In the modern (non-backwardsCompat) path it throws ArgumentException(SR.Image_InsufficientBufferSize) naming sourceBufferSize; in the backwards-compat path it surfaces the WIC error WINCODEC_ERR_INSUFFICIENTBUFFER via HRESULT.Check instead.","triggerScenarios":"WritePixelsImpl called with a buffer whose size is less than sourceRect.Width*sourceRect.Height*bytesPerPixel (per row: less than width*bpp), e.g., stride/height computed for a different pixel format than the bitmap's _format.","commonSituations":"Computing buffer size for BGRA32 while the bitmap is 64-bit (Pbgra64), underestimating stride, or passing IntPtr buffer plus a size constant for another format.","solutions":["Compute sourceBufferSize from the bitmap's actual format: Format.InternalBitsPerPixel / 8 * rect.Width * rect.Height.","Use _format.BytesPerPixel (or source.Format) rather than a hard-coded 4.","Grow or reallocate the native buffer to at least the required size before the call.","In compat paths, catch the HRESULT WINCODEC_ERR_INSUFFICIENTBUFFER and surface a clear message."],"exampleFix":"// before\nint size = rect.Width * rect.Height * 4; // assumed BGRA32\n// after\nint size = rect.Width * rect.Height * (bitmap.Format.BitsPerPixel / 8);","handlingStrategy":"validation","validationCode":"int bpp = bitmap.Format.BitsPerPixel / 8;\nlong required = (long)bpp * sourceRect.Width * sourceRect.Height;\nif (sourceBufferSize < required) throw new ArgumentException($\"need {required} bytes, have {sourceBufferSize}\");","typeGuard":null,"tryCatchPattern":"try { WritePixels(...); } catch (ArgumentException e) when (e.ParamName == \"sourceBufferSize\") { /* reallocate and retry */ }","preventionTips":["Derive size from bitmap.Format, never hard-code 4 bytes/pixel","Recompute sizes when the pixel format changes","Assert buffer size in debug builds before interop calls"],"tags":["wpf","bitmap","buffer-size","argument-exception"],"backgroundTag":"payload-too-large","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}