dotnet/wpf · error · ArgumentException
SR.Collection_BadRank
Error message
SR.Collection_BadRank
What it means
BitmapSource.CriticalCopyPixels throws ArgumentException with SR.Collection_BadRank when the destination pixels array is multi-dimensional (Rank != 1). Pixel copy buffers must be a flat one-dimensional Array so bytes can be copied contiguously.
Solutions
- Allocate a flat 1D array (e.g. new int[width * height]) and pass that.
- Copy into the 1D buffer, then remap into the 2D array row by row afterwards.
- Overload-check: use the CopyPixels overload taking an IntPtr with stride if you must write into custom memory.
Example fix
// before var pixels = new int[height, width]; bitmap.CopyPixels(pixels, stride, 0); // throws // after var pixels = new int[width * height]; bitmap.CopyPixels(pixels, width * 4, 0);
Defensive patterns
Strategy: validation
Validate before calling
if (pixels == null || pixels.Rank != 1)
throw new ArgumentException("pixels must be a 1D array", nameof(pixels)); Type guard
bool IsFlatBuffer<T>(T[] buffer) => buffer != null; // use T[] not T[,]
Try / catch
try { bitmap.CopyPixels(pixels, stride, offset); } catch (ArgumentException ex) { /* flatten array */ } Prevention
- Always allocate pixel buffers as flat 1D arrays
- Never pass T[,] to CopyPixels
- Row-copy into 2D arrays only after the flat CopyPixels call
When it happens
Trigger: Calling bitmapSource.CopyPixels(pixels, ...) with a 2D array such as int[,] or Color[,] as the destination.
Common situations: Developers modeling image rows/columns with int[height,width] because it feels natural, then passing it directly to CopyPixels.
Understand the failure class
Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.
Related errors
- SR.Collection_BadRank
- SR.Collection_BadRank
- SR.Image_AlphaThresholdOutOfRange
- SR.Image_BadPixelFormat
- SR.Image_InvalidArrayForPixel
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/a5f3f8800923ade0.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapSource.cs:643
}
/// <summary>
/// CriticalCopyPixels
/// </summary>
/// <param name="sourceRect"></param>
/// <param name="pixels"></param>
/// <param name="stride"></param>
/// <param name="offset"></param>
internal unsafe void CriticalCopyPixels(Int32Rect sourceRect, Array pixels, int stride, int offset)
{
ReadPreamble();
_bitmapInit.EnsureInitializedComplete();
CompleteDelayedCreation();
ArgumentNullException.ThrowIfNull(pixels);
if (pixels.Rank != 1)
throw new ArgumentException(SR.Collection_BadRank, nameof(pixels));
if (offset < 0)
{
HRESULT.Check((int)WinCodecErrors.WINCODEC_ERR_VALUEOVERFLOW);
}
int elementSize = -1;
if (pixels is byte[])
elementSize = 1;
else if (pixels is short[] || pixels is ushort[])
elementSize = 2;
else if (pixels is int[] || pixels is uint[] || pixels is float[])
elementSize = 4;
else if (pixels is double[])
elementSize = 8;
if (elementSize == -1)View on GitHub (pinned to 81131a70a4)