{"record":{"id":"a5f3f8800923ade0","repo":"dotnet/wpf","slug":"sr-collection-badrank-bitmapsource","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/BitmapSource.cs","lineNumber":643,"sourceCode":"        }\n\n        /// <summary>\n        /// CriticalCopyPixels\n        /// </summary>\n        /// <param name=\"sourceRect\"></param>\n        /// <param name=\"pixels\"></param>\n        /// <param name=\"stride\"></param>\n        /// <param name=\"offset\"></param>\n        internal unsafe void CriticalCopyPixels(Int32Rect sourceRect, Array pixels, int stride, int offset)\n        {\n            ReadPreamble();\n            _bitmapInit.EnsureInitializedComplete();\n            CompleteDelayedCreation();\n\n            ArgumentNullException.ThrowIfNull(pixels);\n\n            if (pixels.Rank != 1)\n                throw new ArgumentException(SR.Collection_BadRank, nameof(pixels));\n\n            if (offset < 0)\n            {\n                HRESULT.Check((int)WinCodecErrors.WINCODEC_ERR_VALUEOVERFLOW);\n            }\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)","sourceCodeStart":625,"sourceCodeEnd":661,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapSource.cs#L625-L661","documentation":"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.","triggerScenarios":"Calling bitmapSource.CopyPixels(pixels, ...) with a 2D array such as int[,] or Color[,] as the destination.","commonSituations":"Developers modeling image rows/columns with int[height,width] because it feels natural, then passing it directly to CopyPixels.","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."],"exampleFix":"// before\nvar pixels = new int[height, width];\nbitmap.CopyPixels(pixels, stride, 0); // throws\n// after\nvar pixels = new int[width * height];\nbitmap.CopyPixels(pixels, width * 4, 0);","handlingStrategy":"validation","validationCode":"if (pixels == null || pixels.Rank != 1)\n    throw new ArgumentException(\"pixels must be a 1D array\", nameof(pixels));","typeGuard":"bool IsFlatBuffer<T>(T[] buffer) => buffer != null; // use T[] not T[,]","tryCatchPattern":"try { bitmap.CopyPixels(pixels, stride, offset); } catch (ArgumentException ex) { /* flatten array */ }","preventionTips":["Always allocate pixel buffers as flat 1D arrays","Never pass T[,] to CopyPixels","Row-copy into 2D arrays only after the flat CopyPixels call"],"tags":["wpf","imaging","array-rank","argument"],"backgroundTag":"invalid-argument-format","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"}