dotnet/machinelearning · error · ArgumentException

Invalid path

Error message

Invalid path

What it means

MLImage.CreateFromFile decodes the file at imagePath with SKBitmap.Decode(string). If Skia returns null — the path exists but its contents cannot be decoded as a bitmap — the method throws an ArgumentException named 'Invalid path' with the imagePath parameter. (A null/empty path throws ArgumentNullException earlier.)

Source

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

            return new MLImage(image);
        }

        /// <summary>
        /// Create a new MLImage instance from a stream.
        /// </summary>
        /// <param name="imagePath">The image file path to create the image from.</param>
        /// <returns>MLImage object.</returns>
        public static MLImage CreateFromFile(string imagePath)
        {
            if (imagePath is null)
            {
                throw new ArgumentNullException(nameof(imagePath));
            }

            SKBitmap image = SKBitmap.Decode(imagePath);
            if (image is null)
            {
                throw new ArgumentException($"Invalid path", nameof(imagePath));
            }

            return new MLImage(image);
        }

        /// <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));

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Verify the file opens in an image viewer / check its magic bytes; replace it with a valid image
  2. Re-download or re-export the image in PNG/JPEG/BMP
  3. Convert non-raster formats (SVG, HEIC) to a raster bitmap format first

Example fix

// before
var img = MLImage.CreateFromFile(svgPath); // SKBitmap can't decode SVG
// after
byte[] head = File.ReadAllBytes(svgPath)[..4];
if (!IsKnownImageMagic(head))
    throw new InvalidDataException($"{svgPath} is not a decodable bitmap; convert it to PNG/JPEG.");
var img = MLImage.CreateFromFile(svgPath);
Defensive patterns

Strategy: validation

Validate before calling

if (!File.Exists(imagePath)) throw new FileNotFoundException(imagePath);
byte[] head = new byte[4];
using (var fs = File.OpenRead(imagePath)) fs.Read(head, 0, 4);
bool magic = head[0] == 0xFF || head[0] == 0x89 || head[0] == 0x42 || head[0] == 0x47;
if (!magic) throw new InvalidDataException($"{imagePath} is not a bitmap image.");

Type guard

bool IsDecodableImageFile(string p) => File.Exists(p) && new FileInfo(p).Length > 0 && new FileInfo(p).Length < int.MaxValue;

Try / catch

try
{
    img = MLImage.CreateFromFile(imagePath);
}
catch (ArgumentException ex) when (ex.ParamName == "imagePath")
{
    logger.LogWarning("Cannot decode image file {Path}", imagePath);
}

Prevention

When it happens

Trigger: Calling MLImage.CreateFromFile on a file whose bytes are not a decodable image: a text/HTML file with an image extension, a truncated download, an unsupported codec, or a zero-byte placeholder file.

Common situations: Scraped/spidered files that are actually 404 pages; files with wrong extensions (e.g. .png that is really WebP or SVG — SVG is not decodable by SKBitmap); corrupted transfers.

Understand the failure class

Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.

Related errors


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