dotnet/wpf · error · ArgumentException

SR.Image_InvalidArrayForPixel

Error message

SR.Image_InvalidArrayForPixel

What it means

The CachedBitmap constructor only supports pixel arrays whose element size is 1, 2, 4, or 8 bytes (byte, short, ushort, int, uint, float, double). If the array is of any other element type (bool, long, decimal, object, custom struct, etc.) the constructor cannot determine the pixel element size and throws this ArgumentException.

Solutions

  1. Convert the array to one of the supported types: byte[], short[], ushort[], int[], uint[], float[], or double[]
  2. Use BitConverter or a cast loop to build an int[]/byte[] from unsupported element types
  3. Check the PixelFormat's bits-per-pixel and pick the matching element size

Example fix

// before
long[] pixels = LoadLongs();
var bmp = new CachedBitmap(pixels, w, h, 96, 96, PixelFormats.Bgr32, null, w * 4);
// after
int[] pixels = LoadLongs().Select(l => unchecked((int)l)).ToArray();
var bmp = new CachedBitmap(pixels, w, h, 96, 96, PixelFormats.Bgr32, null, w * 4);
Defensive patterns

Strategy: type-guard

Validate before calling

static readonly Type[] Supported = { typeof(byte), typeof(short), typeof(ushort), typeof(int), typeof(uint), typeof(float), typeof(double) };
static bool IsSupportedPixelType(Array a) => a != null && Array.IndexOf(Supported, a.GetType().GetElementType()) >= 0;

Type guard

static bool IsBytePixels(Array a) => a is byte[];

Try / catch

try { return new CachedBitmap(pixels, w, h, 96, 96, fmt, palette, stride); }
catch (ArgumentException ex) when (ex.Message.Contains("array")) { pixels = ConvertToSupported(pixels); return new CachedBitmap(pixels, w, h, 96, 96, fmt, palette, stride); }

Prevention

When it happens

Trigger: new CachedBitmap(long[] or bool[] or string[] or customStruct[], ...) — any Array that is 1D but not one of the seven supported element types.

Common situations: Using long[] for 16-bit-per-channel data (long is 8 bytes but a different type than the supported list); passing an object[] collected from LINQ; passing a struct-of-pixels array.

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/1f23f99d49963fb2. Report an issue: GitHub.

Appendix: source

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

        {
            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))
                InitFromMemoryPtr(pixelWidth, pixelHeight, dpiX, dpiY, pixelFormat, palette, (nint)pixelArray, destBufferSize, stride);
        }

        /// <summary>
        /// Common implementation for CloneCore(), CloneCurrentValueCore(),
        /// GetAsFrozenCore(), and GetCurrentValueAsFrozenCore().
        /// </summary>
        private void CopyCommon(CachedBitmap sourceBitmap)
        {
            // Avoid Animatable requesting resource updates for invalidations that occur during construction
            Animatable_IsResourceInvalidationNecessary = false;

            if (sourceBitmap._source != null)
            {

View on GitHub (pinned to 81131a70a4)