dotnet/wpf · error · ArgumentException
SR.Image_InvalidArrayForPixel
Error message
SR.Image_InvalidArrayForPixel
What it means
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.
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.
Example fix
// before
class Pixel { public byte B, G, R, A; }
var px = new Pixel[w*h];
bitmap.WritePixels(rect, px, stride, 0); // throws
// after
struct Pixel { public byte B, G, R, A; }
var px = new Pixel[w*h];
bitmap.WritePixels(rect, px, stride, 0); Defensive patterns
Strategy: type-guard
Validate before calling
bool ok = sourceBuffer != null && sourceBuffer.GetType().GetElementType()?.IsValueType == true;
Type guard
bool IsValueTypeArray<T>(T[] buf) where T : struct => buf != null; // constrain generics: void WritePixels<T>(Rect r, T[] buf, ...) where T : struct
Try / catch
try { bitmap.WritePixels(rect, buf, stride, 0); } catch (ArgumentException e) when (e.Message.Contains("pixel")) { /* convert to value-type buffer */ } Prevention
- Use structs with [StructLayout(LayoutKind.Sequential)] for pixel data
- Never pass object[] or class arrays to WritePixels
- Constrain generic helpers with 'where T : struct'
When it happens
Trigger: Calling WritePixels with a class-typed array such as object[], string[], or a struct array boxed incorrectly — the resolved elementType is not IsValueType.
Common situations: 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.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- ArgumentOutOfRangeException (timeout was Duration.Automatic)
- SR.Image_InsufficientBuffer
- SR.Image_InsufficientBufferSize
- ArgumentNullException (buffer/sourceBuffer was IntPtr.Zero)
- Collection_BadRank
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/8b95ad0e91269b81.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/WriteableBitmap.cs:392
Int32Rect sourceRect,
Array sourceBuffer,
int sourceBufferStride,
int destinationX,
int destinationY
)
{
WritePreamble();
ValidateArrayAndGetInfo(sourceBuffer,
backwardsCompat: false,
out _,
out uint sourceBufferSize,
out Type elementType);
// We accept arrays of arbitrary value types - but not reference types.
if (elementType == null || !elementType.IsValueType)
{
throw new ArgumentException(SR.Image_InvalidArrayForPixel);
}
// Get the address of the data in the array by pinning it.
unsafe
{
fixed (byte* buffer = &MemoryMarshal.GetArrayDataReference(sourceBuffer))
WritePixelsImpl(sourceRect,
(nint)buffer,
sourceBufferSize,
sourceBufferStride,
destinationX,
destinationY,
backwardsCompat: false);
}
}
/// <summary>
/// Update the pixels of this BitmapView on GitHub (pinned to 81131a70a4)