dotnet/wpf · error · ArgumentException

SR.Collection_BadRank

Error message

SR.Collection_BadRank

What it means

CachedBitmap's constructor accepts only a one-dimensional Array as the pixels parameter. The library throws this ArgumentException when pixels.Rank != 1 because multidimensional or jagged arrays have a memory layout the pixel-copy code cannot use directly. Pass a flat 1D array in the bitmap's native element type instead.

Solutions

  1. Flatten the multidimensional array into a 1D array before constructing the CachedBitmap
  2. Wrap the copy in a loop: for (int y=0;y<h;y++) for (int x=0;x<w;x++) flat[y*w+x]=src[y,x];
  3. Pass the flattened array plus the correct pixelWidth, pixelHeight and stride to the constructor

Example fix

// before
byte[,] gray = new byte[h, w];
var bmp = new CachedBitmap(gray, w, h, 96, 96, PixelFormats.Gray8, null, w);
// after
byte[] flat = new byte[w * h];
for (int y = 0; y < h; y++)
    for (int x = 0; x < w; x++)
        flat[y * w + x] = gray[y, x];
var bmp = new CachedBitmap(flat, w, h, 96, 96, PixelFormats.Gray8, null, w);
Defensive patterns

Strategy: validation

Validate before calling

static bool IsFlatPixelArray(Array a) => a != null && a.Rank == 1 && a.GetType().GetElementType() is Type t && (t == typeof(byte) || t == typeof(short) || t == typeof(ushort) || t == typeof(int) || t == typeof(uint) || t == typeof(float) || t == typeof(double));

Type guard

static bool Is1DArrayOf<T>(Array a) => a is T[];

Prevention

When it happens

Trigger: Calling new CachedBitmap(byte[,], ...), a short[,,], or a jagged array (e.g. byte[][]) as the first constructor argument; any array whose Array.Rank is not 1.

Common situations: Developers build a 2D height/intensity map (e.g. grayscale[x,y]) and pass it directly; data deserialized from a multidim array; porting GDI+ LockBits code that used 2D arrays.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/8cbf1fa7bcd9e869. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/CachedBitmap.cs:116

        /// <summary>
        /// </summary>
        internal unsafe CachedBitmap(
            int pixelWidth,
            int pixelHeight,
            double dpiX,
            double dpiY,
            PixelFormat pixelFormat,
            BitmapPalette palette,
            System.Array pixels,
            int stride
            )
            : base(true) // Use base class virtuals
        {
            ArgumentNullException.ThrowIfNull(pixels);

            if (pixels.Rank != 1)
                throw new ArgumentException(SR.Collection_BadRank, nameof(pixels));

            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)
                throw new ArgumentException(SR.Image_InvalidArrayForPixel);

            int destBufferSize = elementSize * pixels.Length;

            fixed (byte* pixelArray = &MemoryMarshal.GetArrayDataReference(pixels))

View on GitHub (pinned to 81131a70a4)