dotnet/machinelearning · error · ArgumentException
Invalid width value.
Error message
Invalid width value.
What it means
MLImage.CreateFromPixels validates that the width is strictly positive after checking the pixel format; width <= 0 throws an ArgumentException 'Invalid width value.' naming the width parameter, because a bitmap cannot have a non-positive dimension.
Source
Thrown at src/Microsoft.ML.ImageAnalytics/MLImage.cs:90
/// <summary>
/// Creates MLImage object from the pixel data span.
/// </summary>
/// <param name="width">The width of the image in pixels.</param>
/// <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));View on GitHub (pinned to 7b76e69cf9)
Solutions
- Ensure width >= 1 (and height >= 1) before the call
- Validate parsed dimensions at the data boundary and reject/skip frames with non-positive size
- Check the source of the dimension (header parse, division) for logic errors
Example fix
// before
int w = header.Width / scale; // can be 0
var img = MLImage.CreateFromPixels(w, h, MLPixelFormat.Bgra32, data);
// after
if (w <= 0 || h <= 0)
throw new InvalidOperationException($"Cannot create image of size {w}x{h}.");
var img = MLImage.CreateFromPixels(w, h, MLPixelFormat.Bgra32, data); Defensive patterns
Strategy: validation
Validate before calling
if (width <= 0)
throw new ArgumentOutOfRangeException(nameof(width), width, "Width 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 == "width")
{
logger.LogError(ex, "Invalid image width {W}", width);
} Prevention
- Validate dimensions when parsed from headers/metadata
- Watch for division truncation producing 0
- Check for swapped width/height arguments
When it happens
Trigger: Calling MLImage.CreateFromPixels with width = 0 or negative — e.g. an uninitialized dimension variable, integer division truncation yielding 0, or swapped/negative parsed values from config.
Common situations: Parsing width from a header/JSON where 0 means 'unknown'; computing width from a failed decode or empty metadata; sign errors when converting from unsigned sizes.
Related errors
- Invalid height value.
- Directory "{0}" does not exist.
- Unsupported pixel format
- Either input_ids or inputs_embeds must be set
- Dimension must be divisible by 2
AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11).
Data as JSON: /api/errors/e8ed73f378840528.
Report an issue: GitHub.