{"record":{"id":"8cbf1fa7bcd9e869","repo":"dotnet/wpf","slug":"sr-collection-badrank-cachedbitmap","errorCode":null,"errorMessage":"SR.Collection_BadRank","messagePattern":"SR\\.Collection_BadRank","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/CachedBitmap.cs","lineNumber":116,"sourceCode":"\n        /// <summary>\n        /// </summary>\n        internal unsafe CachedBitmap(\n            int pixelWidth,\n            int pixelHeight,\n            double dpiX,\n            double dpiY,\n            PixelFormat pixelFormat,\n            BitmapPalette palette,\n            System.Array pixels,\n            int stride\n            )\n            : base(true) // Use base class virtuals\n        {\n            ArgumentNullException.ThrowIfNull(pixels);\n\n            if (pixels.Rank != 1)\n                throw new ArgumentException(SR.Collection_BadRank, nameof(pixels));\n\n            int elementSize = -1;\n\n            if (pixels is byte[])\n                elementSize = 1;\n            else if (pixels is short[] || pixels is ushort[])\n                elementSize = 2;\n            else if (pixels is int[] || pixels is uint[] || pixels is float[])\n                elementSize = 4;\n            else if (pixels is double[])\n                elementSize = 8;\n\n            if (elementSize == -1)\n                throw new ArgumentException(SR.Image_InvalidArrayForPixel);\n\n            int destBufferSize = elementSize * pixels.Length;\n\n            fixed (byte* pixelArray = &MemoryMarshal.GetArrayDataReference(pixels))","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/CachedBitmap.cs#L98-L134","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Flatten the multidimensional array into a 1D array before constructing the CachedBitmap","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];","Pass the flattened array plus the correct pixelWidth, pixelHeight and stride to the constructor"],"exampleFix":"// before\nbyte[,] gray = new byte[h, w];\nvar bmp = new CachedBitmap(gray, w, h, 96, 96, PixelFormats.Gray8, null, w);\n// after\nbyte[] flat = new byte[w * h];\nfor (int y = 0; y < h; y++)\n    for (int x = 0; x < w; x++)\n        flat[y * w + x] = gray[y, x];\nvar bmp = new CachedBitmap(flat, w, h, 96, 96, PixelFormats.Gray8, null, w);","handlingStrategy":"validation","validationCode":"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));","typeGuard":"static bool Is1DArrayOf<T>(Array a) => a is T[];","tryCatchPattern":null,"preventionTips":["Always store raw pixel data in flat 1D arrays with an explicit stride","Never pass T[,] or T[][] to APIs expecting pixel buffers","Check Array.Rank in helper wrappers before constructing bitmaps"],"tags":["wpf","imaging","argument"],"backgroundTag":"type-mismatch","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"}