{"record":{"id":"e5da05b6d5227d4e","repo":"dotnet/machinelearning","slug":"invalid-path","errorCode":null,"errorMessage":"Invalid path","messagePattern":"Invalid path","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.ImageAnalytics/MLImage.cs","lineNumber":67,"sourceCode":"            return new MLImage(image);\n        }\n\n        /// <summary>\n        /// Create a new MLImage instance from a stream.\n        /// </summary>\n        /// <param name=\"imagePath\">The image file path to create the image from.</param>\n        /// <returns>MLImage object.</returns>\n        public static MLImage CreateFromFile(string imagePath)\n        {\n            if (imagePath is null)\n            {\n                throw new ArgumentNullException(nameof(imagePath));\n            }\n\n            SKBitmap image = SKBitmap.Decode(imagePath);\n            if (image is null)\n            {\n                throw new ArgumentException($\"Invalid path\", nameof(imagePath));\n            }\n\n            return new MLImage(image);\n        }\n\n        /// <summary>\n        /// Creates MLImage object from the pixel data span.\n        /// </summary>\n        /// <param name=\"width\">The width of the image in pixels.</param>\n        /// <param name=\"height\">The height of the image in pixels.</param>\n        /// <param name=\"pixelFormat\">The pixel format to create the image with.</param>\n        /// <param name=\"imagePixelData\">The pixels data to create the image from.</param>\n        /// <returns>MLImage object.</returns>\n        public static unsafe MLImage CreateFromPixels(int width, int height, MLPixelFormat pixelFormat, ReadOnlySpan<byte> imagePixelData)\n        {\n            if (pixelFormat != MLPixelFormat.Bgra32 && pixelFormat != MLPixelFormat.Rgba32)\n            {\n                throw new ArgumentException($\"Unsupported pixel format\", nameof(pixelFormat));","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.ImageAnalytics/MLImage.cs#L49-L85","documentation":"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.)","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify the file opens in an image viewer / check its magic bytes; replace it with a valid image","Re-download or re-export the image in PNG/JPEG/BMP","Convert non-raster formats (SVG, HEIC) to a raster bitmap format first"],"exampleFix":"// before\nvar img = MLImage.CreateFromFile(svgPath); // SKBitmap can't decode SVG\n// after\nbyte[] head = File.ReadAllBytes(svgPath)[..4];\nif (!IsKnownImageMagic(head))\n    throw new InvalidDataException($\"{svgPath} is not a decodable bitmap; convert it to PNG/JPEG.\");\nvar img = MLImage.CreateFromFile(svgPath);","handlingStrategy":"validation","validationCode":"if (!File.Exists(imagePath)) throw new FileNotFoundException(imagePath);\nbyte[] head = new byte[4];\nusing (var fs = File.OpenRead(imagePath)) fs.Read(head, 0, 4);\nbool magic = head[0] == 0xFF || head[0] == 0x89 || head[0] == 0x42 || head[0] == 0x47;\nif (!magic) throw new InvalidDataException($\"{imagePath} is not a bitmap image.\");","typeGuard":"bool IsDecodableImageFile(string p) => File.Exists(p) && new FileInfo(p).Length > 0 && new FileInfo(p).Length < int.MaxValue;","tryCatchPattern":"try\n{\n    img = MLImage.CreateFromFile(imagePath);\n}\ncatch (ArgumentException ex) when (ex.ParamName == \"imagePath\")\n{\n    logger.LogWarning(\"Cannot decode image file {Path}\", imagePath);\n}","preventionTips":["Never trust file extensions; verify magic bytes","Quarantine undecodable files rather than letting the pipeline crash","Convert SVG/HEIC sources to PNG/JPEG before ingestion"],"tags":["csharp","images","skia","decoding"],"backgroundTag":"file-read-failed","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}