{"record":{"id":"019c09b542f73fe7","repo":"dotnet/wpf","slug":"sr-image-invalidarrayforpixel","errorCode":null,"errorMessage":"SR.Image_InvalidArrayForPixel","messagePattern":"SR\\.Image_InvalidArrayForPixel","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapSource.cs","lineNumber":662,"sourceCode":"\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)\n                throw new ArgumentException(SR.Image_InvalidArrayForPixel);\n\n            uint destBufferSize = checked((uint)elementSize * (uint)(pixels.Length - offset));\n\n            // Check whether offset is out of bounds manually\n            if (offset >= pixels.Length)\n                throw new IndexOutOfRangeException();\n\n            fixed (byte* pixelArray = &Unsafe.AddByteOffset(ref MemoryMarshal.GetArrayDataReference(pixels), (nint)offset * elementSize))\n                CriticalCopyPixels(sourceRect, (nint)pixelArray, destBufferSize, stride);\n        }\n\n        /// <summary>\n        /// CriticalCopyPixels\n        /// </summary>\n        /// <param name=\"sourceRect\"></param>\n        /// <param name=\"buffer\"></param>\n        /// <param name=\"bufferSize\"></param>\n        /// <param name=\"stride\"></param>","sourceCodeStart":644,"sourceCodeEnd":680,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapSource.cs#L644-L680","documentation":"BitmapSource.CriticalCopyPixels determines the element size of the destination buffer by type (byte=1, short/ushort=2, int/uint/float=4, double=8) and throws ArgumentException with SR.Image_InvalidArrayForPixel when the array's element type is unsupported (elementSize stays -1). Only these primitive element types can be copied into.","triggerScenarios":"Calling CopyPixels with an array of Color, long, decimal, or custom struct elements, e.g. pixels = new Color[width*height].","commonSituations":"Assuming CopyPixels accepts Color[] because the palette uses colors; using long[] to 'avoid overflow'; generic helper methods with Array parameters that end up holding unsupported element types.","solutions":["Use byte[] (most common, stride = width * bytesPerPixel) or another supported primitive array (short[], int[], uint[], float[], double[]).","Copy into byte[] then convert to the desired type afterwards.","Add a type check (buffer is byte[] || short[] || int[] || uint[] || float[] || double[]) in helper code before calling CopyPixels."],"exampleFix":"// before\nvar pixels = new Color[width * height];\nbitmap.CopyPixels(pixels, stride, 0); // throws\n// after\nvar pixels = new byte[height * stride];\nbitmap.CopyPixels(pixels, stride, 0);","handlingStrategy":"type-guard","validationCode":"bool supported = pixels is byte[] or short[] or ushort[] or int[] or uint[] or float[] or double[];\nif (!supported) throw new ArgumentException(\"unsupported pixel buffer type\");","typeGuard":"bool IsSupportedPixelBuffer(Array a) => a is byte[] or short[] or ushort[] or int[] or uint[] or float[] or double[];","tryCatchPattern":"try { bitmap.CopyPixels(buffer, stride, 0); } catch (ArgumentException) { buffer = new byte[bitmap.PixelHeight * stride]; bitmap.CopyPixels(buffer, stride, 0); }","preventionTips":["Default to byte[] buffers","Check element type of helper methods' Array parameters","Never pass Color[] or long[] to CopyPixels"],"tags":["wpf","imaging","type-mismatch","array"],"backgroundTag":"type-mismatch","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}