{"record":{"id":"d495e17e6e29d3dd","repo":"dotnet/wpf","slug":"sr-image-insufficientbuffer","errorCode":null,"errorMessage":"SR.Image_InsufficientBuffer","messagePattern":"SR\\.Image_InsufficientBuffer","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/WriteableBitmap.cs","lineNumber":1048,"sourceCode":"        /// </param>\n        /// <param name=\"elementSize\">\n        ///     On output, will contain the size of the elements in the array.\n        /// </param>\n        /// <param name=\"sourceBufferSize\">\n        ///     On output, will contain the size of the array.\n        /// </param>\n        private static void ValidateArrayAndGetInfo(Array sourceBuffer,\n                                                    bool backwardsCompat,\n                                                    out int elementSize,\n                                                    out uint sourceBufferSize,\n                                                    out Type elementType)\n        {\n            //\n            // Assure that a valid pixels Array was provided.\n            //\n            if (sourceBuffer == null)\n            {\n                throw new ArgumentNullException(backwardsCompat ? \"pixels\" : \"sourceBuffer\");\n            }\n\n            if (sourceBuffer.Rank == 1)\n            {\n                int firstDimLength = sourceBuffer.GetLength(0);\n                if (firstDimLength == 0)\n                {\n                    if (backwardsCompat)\n                    {\n                        elementSize = 1;\n                        sourceBufferSize = 0;\n                        elementType = null;\n                    }\n                    else\n                    {\n                        throw new ArgumentException(SR.Image_InsufficientBuffer, nameof(sourceBuffer));\n                    }\n                }","sourceCodeStart":1030,"sourceCodeEnd":1066,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/WriteableBitmap.cs#L1030-L1066","documentation":"ValidateArrayAndGetInfo checks the pixels array before copying. For a rank-1 array, a zero-length first dimension cannot supply any pixel data, so it throws ArgumentException(SR.Image_InsufficientBuffer) (or, per the shown region, ArgumentNullException first for a null buffer named 'pixels'/'sourceBuffer'). This guards the pin/copy path from zero-byte buffers.","triggerScenarios":"Calling WritePixels with a null array, or new T[0] / any rank-1 array whose first dimension is 0.","commonSituations":"Array allocated with the wrong dimension order (e.g., new byte[0][rows]); buffers sized from an empty sourceRect; failed deserialization yielding empty arrays.","solutions":["Pass a non-null array with first dimension >= sourceRect.Height (or required element count).","Validate array.Length > 0 and enough elements for rect*stride before calling.","Guard the call: if (pixels == null || pixels.Length == 0) skip or throw a clearer error."],"exampleFix":"// before\nvar pixels = new byte[0];\nbitmap.WritePixels(rect, pixels, stride, 0); // throws\n// after\nvar pixels = new byte[stride * rect.Height];\nbitmap.WritePixels(rect, pixels, stride, 0);","handlingStrategy":"validation","validationCode":"if (sourceBuffer == null) throw new ArgumentNullException(nameof(sourceBuffer));\nif (sourceBuffer.Length == 0) throw new ArgumentException(\"sourceBuffer must not be empty\");\nif (sourceBuffer.Length < sourceRect.Height * stride / elementSize) throw new ArgumentException(\"sourceBuffer too small\");","typeGuard":"bool IsValidPixels<T>(T[] buf) where T : struct => buf != null && buf.Length > 0;","tryCatchPattern":"try { bitmap.WritePixels(rect, buf, stride, 0); } catch (ArgumentException e) when (e.ParamName == \"sourceBuffer\") { /* fix buffer */ }","preventionTips":["Never pass zero-length arrays","Check array dimensions match the destination rect","Guard against empty arrays from deserializers"],"tags":["wpf","bitmap","argument-exception","empty-array"],"backgroundTag":"empty-required-field","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"}