stride3d/stride · error · ArgumentException

Invalid array slice index

Error message

Invalid array slice index

What it means

Thrown by Image.GetPixelBuffer for non-3D textures when arrayOrZSliceIndex exceeds Description.ArraySize. For 1D/2D textures the parameter selects an array slice and must be within the declared array size.

Solutions

  1. Loop with arraySlice < image.Description.ArraySize
  2. Verify the texture was created as a texture array / cubemap (ArraySize 6 for cubemaps) if face access is intended
  3. Recreate the image with a larger ArraySize if more slices are required

Example fix

// before
for (int slice = 0; slice <= image.Description.ArraySize; slice++)
    var pb = image.GetPixelBuffer(slice, 0);
// after
for (int slice = 0; slice < image.Description.ArraySize; slice++)
    var pb = image.GetPixelBuffer(slice, 0);
Defensive patterns

Strategy: validation

Validate before calling

if (image.Description.Dimension != TextureDimension.Texture3D && (arraySlice < 0 || arraySlice >= image.Description.ArraySize))
    throw new ArgumentOutOfRangeException(nameof(arraySlice), arraySlice, $"Array slice must be in [0,{image.Description.ArraySize})");

Type guard

bool IsValidArraySlice(Image image, int slice) =>
    image.Description.Dimension != TextureDimension.Texture3D
        ? slice >= 0 && slice < image.Description.ArraySize
        : slice >= 0 && slice < image.Description.Depth;

Try / catch

try
{
    var pb = image.GetPixelBuffer(arraySlice, mip);
}
catch (ArgumentException ex) when (ex.ParamName == "arrayOrZSliceIndex")
{
    log.Warn($"Array slice {arraySlice} out of range (ArraySize={image.Description.ArraySize})");
    return null;
}

Prevention

When it happens

Trigger: image.GetPixelBuffer(arraySlice, mip) on a 1D/2D texture where arraySlice >= Description.ArraySize, e.g. assuming ArraySize is a count starting at 1 or iterating a cube-map face index (6) on a texture with ArraySize 1.

Common situations: Off-by-one when ArraySize is a count vs. a max index; reading cube-map faces from a non-cubemap texture; sharing code between texture arrays and single textures without checking Description.ArraySize.

Related errors


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

Appendix: source

Thrown at sources/engine/Stride.Foundation/Graphics/Image.cs:235

        /// <exception cref="ArgumentException">If arrayOrZSliceIndex or mipmap are out of range.</exception>
        public PixelBuffer GetPixelBuffer(int arrayOrZSliceIndex, int mipmap)
        {
            // Check for parameters, as it is easy to mess up things...
            if (mipmap > Description.MipLevels)
                throw new ArgumentException("Invalid mipmap level", nameof(mipmap));

            if (Description.Dimension == TextureDimension.Texture3D)
            {
                if (arrayOrZSliceIndex > Description.Depth)
                    throw new ArgumentException("Invalid z slice index", nameof(arrayOrZSliceIndex));

                // For 3D textures
                return GetPixelBufferUnsafe(0, arrayOrZSliceIndex, mipmap);
            }

            if (arrayOrZSliceIndex > Description.ArraySize)
            {
                throw new ArgumentException("Invalid array slice index", nameof(arrayOrZSliceIndex));
            }

            // For 1D, 2D textures
            return GetPixelBufferUnsafe(arrayOrZSliceIndex, 0, mipmap);
        }

        /// <summary>
        /// Gets the pixel buffer for the specified array/z slice and mipmap level.
        /// </summary>
        /// <param name="arrayIndex">Index into the texture array. Must be set to 0 for 3D images.</param>
        /// <param name="zIndex">Z index for 3D image. Must be set to 0 for all 1D/2D images.</param>
        /// <param name="mipmap">The mipmap.</param>
        /// <returns>A <see cref="Graphics.PixelBuffer"/>.</returns>
        /// <exception cref="ArgumentException">If arrayIndex, zIndex or mipmap are out of range.</exception>
        public PixelBuffer GetPixelBuffer(int arrayIndex, int zIndex, int mipmap)
        {
            // Check for parameters, as it is easy to mess up things...
            if (mipmap > Description.MipLevels)

View on GitHub (pinned to 96fad776d2)