dotnet/wpf · error · ArgumentException

SR.Image_InvalidArrayForPixel

Error message

SR.Image_InvalidArrayForPixel

What it means

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.

Solutions

  1. Use byte[] (most common, stride = width * bytesPerPixel) or another supported primitive array (short[], int[], uint[], float[], double[]).
  2. Copy into byte[] then convert to the desired type afterwards.
  3. Add a type check (buffer is byte[] || short[] || int[] || uint[] || float[] || double[]) in helper code before calling CopyPixels.

Example fix

// before
var pixels = new Color[width * height];
bitmap.CopyPixels(pixels, stride, 0); // throws
// after
var pixels = new byte[height * stride];
bitmap.CopyPixels(pixels, stride, 0);
Defensive patterns

Strategy: type-guard

Validate before calling

bool supported = pixels is byte[] or short[] or ushort[] or int[] or uint[] or float[] or double[];
if (!supported) throw new ArgumentException("unsupported pixel buffer type");

Type guard

bool IsSupportedPixelBuffer(Array a) => a is byte[] or short[] or ushort[] or int[] or uint[] or float[] or double[];

Try / catch

try { bitmap.CopyPixels(buffer, stride, 0); } catch (ArgumentException) { buffer = new byte[bitmap.PixelHeight * stride]; bitmap.CopyPixels(buffer, stride, 0); }

Prevention

When it happens

Trigger: Calling CopyPixels with an array of Color, long, decimal, or custom struct elements, e.g. pixels = new Color[width*height].

Common situations: 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.

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/019c09b542f73fe7. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapSource.cs:662

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

            uint destBufferSize = checked((uint)elementSize * (uint)(pixels.Length - offset));

            // Check whether offset is out of bounds manually
            if (offset >= pixels.Length)
                throw new IndexOutOfRangeException();

            fixed (byte* pixelArray = &Unsafe.AddByteOffset(ref MemoryMarshal.GetArrayDataReference(pixels), (nint)offset * elementSize))
                CriticalCopyPixels(sourceRect, (nint)pixelArray, destBufferSize, stride);
        }

        /// <summary>
        /// CriticalCopyPixels
        /// </summary>
        /// <param name="sourceRect"></param>
        /// <param name="buffer"></param>
        /// <param name="bufferSize"></param>
        /// <param name="stride"></param>

View on GitHub (pinned to 81131a70a4)