{"record":{"id":"8b95ad0e91269b81","repo":"dotnet/wpf","slug":"sr-image-invalidarrayforpixel-writeablebitmap","errorCode":null,"errorMessage":"SR.Image_InvalidArrayForPixel","messagePattern":"SR\\.Image_InvalidArrayForPixel","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/WriteableBitmap.cs","lineNumber":392,"sourceCode":"            Int32Rect sourceRect,\n            Array     sourceBuffer,\n            int       sourceBufferStride,\n            int       destinationX,\n            int       destinationY\n            )\n        {\n            WritePreamble();\n\n            ValidateArrayAndGetInfo(sourceBuffer,\n                                    backwardsCompat: false,\n                                    out _,\n                                    out uint sourceBufferSize,\n                                    out Type elementType);\n\n            // We accept arrays of arbitrary value types - but not reference types.\n            if (elementType == null || !elementType.IsValueType)\n            {\n                throw new ArgumentException(SR.Image_InvalidArrayForPixel);\n            }\n\n            // Get the address of the data in the array by pinning it.\n            unsafe\n            {\n                fixed (byte* buffer = &MemoryMarshal.GetArrayDataReference(sourceBuffer))\n                    WritePixelsImpl(sourceRect,\n                                    (nint)buffer,\n                                    sourceBufferSize,\n                                    sourceBufferStride,\n                                    destinationX,\n                                    destinationY,\n                                    backwardsCompat: false);\n            }\n        }\n\n        /// <summary>\n        /// Update the pixels of this Bitmap","sourceCodeStart":374,"sourceCodeEnd":410,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/WriteableBitmap.cs#L374-L410","documentation":"The generic WritePixels(Array, ...) overload accepts arrays of arbitrary value types (byte[], int[], PixelColor[], etc.) but rejects arrays of reference types, since pixels are copied bitwise from array memory. If the array's element type is null or not a value type, it throws ArgumentException with SR.Image_InvalidArrayForPixel.","triggerScenarios":"Calling WritePixels with a class-typed array such as object[], string[], or a struct array boxed incorrectly — the resolved elementType is not IsValueType.","commonSituations":"Passing arrays of custom classes that look like pixel structs but are reference types; passing object[] holding ints; reflection-built arrays with unknowable element type.","solutions":["Use a struct array (e.g., byte[], uint[], or a struct with LayoutKind.Sequential) instead of class-typed arrays.","Define pixel representation as a struct with explicit layout matching the pixel format.","Convert class-based pixel data into a flat value-type buffer before calling WritePixels."],"exampleFix":"// before\nclass Pixel { public byte B, G, R, A; }\nvar px = new Pixel[w*h];\nbitmap.WritePixels(rect, px, stride, 0); // throws\n// after\nstruct Pixel { public byte B, G, R, A; }\nvar px = new Pixel[w*h];\nbitmap.WritePixels(rect, px, stride, 0);","handlingStrategy":"type-guard","validationCode":"bool ok = sourceBuffer != null && sourceBuffer.GetType().GetElementType()?.IsValueType == true;","typeGuard":"bool IsValueTypeArray<T>(T[] buf) where T : struct => buf != null; // constrain generics: void WritePixels<T>(Rect r, T[] buf, ...) where T : struct","tryCatchPattern":"try { bitmap.WritePixels(rect, buf, stride, 0); } catch (ArgumentException e) when (e.Message.Contains(\"pixel\")) { /* convert to value-type buffer */ }","preventionTips":["Use structs with [StructLayout(LayoutKind.Sequential)] for pixel data","Never pass object[] or class arrays to WritePixels","Constrain generic helpers with 'where T : struct'"],"tags":["wpf","bitmap","argument-exception","value-type"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}