stride3d/stride · error · ArgumentException

Invalid destination pixelBufferArray. Must have the same…

Error message

Invalid destination pixelBufferArray. Must have the same Width, Height and Format.

What it means

PixelBuffer.CopyTo only supports copying between buffers with identical Width, Height, and PixelFormat; it is a raw memory copy, not a rescaling or format-conversion routine. When the destination's dimensions or format differ, it throws ArgumentException for the pixelBuffer argument.

Solutions

  1. Ensure destination and source have the same Width, Height, and Format before copying (check pixelBuffer.Width/Height/Format).
  2. Use format-conversion APIs instead of CopyTo when the pixel formats differ.
  3. Create the destination buffer via an Image/PixelData factory that matches the source description.

Example fix

// before
srcBuffer.CopyTo(destBuffer);
// after
if (destBuffer.Format != srcBuffer.Format || destBuffer.Width != srcBuffer.Width || destBuffer.Height != srcBuffer.Height)
    destBuffer = new Image(srcBuffer.Format, srcBuffer.Width, srcBuffer.Height).GetPixelBuffer();
srcBuffer.CopyTo(destBuffer);
Defensive patterns

Strategy: validation

Validate before calling

bool CanCopy(PixelBuffer src, PixelBuffer dst) =>
    src.Width == dst.Width && src.Height == dst.Height && src.Format == dst.Format;

Try / catch

try { src.CopyTo(dst); }
catch (ArgumentException ex) when (ex.ParamName == "pixelBuffer") { dst = RecreateMatchingBuffer(src); src.CopyTo(dst); }

Prevention

When it happens

Trigger: Calling sourcePixelBuffer.CopyTo(dest) where dest.Width/Height/Format differs from the source — e.g. copying an R8G8B8A8_UNorm buffer into an sRGB-format buffer, or copying into a mip level of different dimensions.

Common situations: See trigger scenarios.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/9c46cbad8f9f616a. Report an issue: GitHub.

Appendix: source

Thrown at sources/engine/Stride.Foundation/Graphics/PixelBuffer.cs:147

        /// <value>The pointer to the pixel buffer.</value>
        public IntPtr DataPointer { get { return this.dataPointer; } }

        /// <summary>
        /// Copies this pixel buffer to a destination pixel buffer.
        /// </summary>
        /// <param name="pixelBuffer">The destination pixel buffer.</param>
        /// <remarks>
        /// The destination pixel buffer must have exactly the same dimensions (width, height) and format than this instance.
        /// Destination buffer can have different row stride.
        /// </remarks>
        public unsafe void CopyTo(PixelBuffer pixelBuffer)
        {
            // Check that buffers are identical
            if (this.Width != pixelBuffer.Width
                || this.Height != pixelBuffer.Height
                || this.Format != pixelBuffer.Format)
            {
                throw new ArgumentException("Invalid destination pixelBufferArray. Must have the same Width, Height and Format.", "pixelBuffer");
            }

            // If buffers have same size, than we can copy it directly
            if (this.BufferStride == pixelBuffer.BufferStride)
            {
                MemoryUtilities.CopyWithAlignmentFallback((void*)pixelBuffer.DataPointer, source: (void*)DataPointer, (uint)BufferStride);
            }
            else
            {
                var srcPointer = (byte*)this.DataPointer;
                var dstPointer = (byte*)pixelBuffer.DataPointer;
                var rowStride = Math.Min(RowStride, pixelBuffer.RowStride);

                // Copy per scanline
                for (int i = 0; i < Height; i++)
                {
                    MemoryUtilities.CopyWithAlignmentFallback(dstPointer, srcPointer, (uint)rowStride);
                    srcPointer += this.RowStride;

View on GitHub (pinned to 96fad776d2)