dotnet/machinelearning · error · ArgumentException

Invalid height value.

Error message

Invalid height value.

What it means

MLImage.CreateFromPixels validates that the height is strictly positive after validating width; height <= 0 throws an ArgumentException 'Invalid height value.' naming the height parameter, since bitmap dimensions must be positive.

Source

Thrown at src/Microsoft.ML.ImageAnalytics/MLImage.cs:95

        /// <param name="height">The height of the image in pixels.</param>
        /// <param name="pixelFormat">The pixel format to create the image with.</param>
        /// <param name="imagePixelData">The pixels data to create the image from.</param>
        /// <returns>MLImage object.</returns>
        public static unsafe MLImage CreateFromPixels(int width, int height, MLPixelFormat pixelFormat, ReadOnlySpan<byte> imagePixelData)
        {
            if (pixelFormat != MLPixelFormat.Bgra32 && pixelFormat != MLPixelFormat.Rgba32)
            {
                throw new ArgumentException($"Unsupported pixel format", nameof(pixelFormat));
            }

            if (width <= 0)
            {
                throw new ArgumentException($"Invalid width value.", nameof(width));
            }

            if (height <= 0)
            {
                throw new ArgumentException($"Invalid height value.", nameof(height));
            }

            if (imagePixelData.Length != width * height * 4)
            {
                throw new ArgumentException($"Invalid {nameof(imagePixelData)} buffer size.");
            }

            SKBitmap image = new SKBitmap(new SKImageInfo(width, height, pixelFormat == MLPixelFormat.Bgra32 ? SKColorType.Bgra8888 : SKColorType.Rgba8888));

            Debug.Assert(image.Info.BitsPerPixel == 32);
            Debug.Assert(image.RowBytes * image.Height == width * height * 4);

            imagePixelData.CopyTo(new Span<byte>(image.GetPixels().ToPointer(), image.Width * image.Height * 4));

            return new MLImage(image);
        }

        /// <summary>

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Ensure height >= 1 before the call
  2. Check argument order — width and height may be swapped at the call site
  3. Validate decoded/probed dimensions before constructing the image

Example fix

// before
var img = MLImage.CreateFromPixels(w, header.Height, MLPixelFormat.Bgra32, data); // Height may be 0
// after
if (header.Height <= 0)
    throw new InvalidOperationException("Source image has invalid height.");
var img = MLImage.CreateFromPixels(w, header.Height, MLPixelFormat.Bgra32, data);
Defensive patterns

Strategy: validation

Validate before calling

if (height <= 0)
    throw new ArgumentOutOfRangeException(nameof(height), height, "Height must be positive.");

Type guard

bool IsValidImageDimensions(int w, int h) => w > 0 && h > 0;

Try / catch

try
{
    img = MLImage.CreateFromPixels(width, height, pixelFormat, data);
}
catch (ArgumentException ex) when (ex.ParamName == "height")
{
    logger.LogError(ex, "Invalid image height {H}", height);
}

Prevention

When it happens

Trigger: Calling MLImage.CreateFromPixels with height = 0 or negative — uninitialized value, off-by-one or division truncation, or a swapped width/height argument order.

Common situations: Swapping width and height when converting between row-major/column-major representations; metadata reporting 0 height for streams not yet probed.

Related errors


AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11). Data as JSON: /api/errors/2a08c54e9b0bbed0. Report an issue: GitHub.