{"record":{"id":"8aa4d72d849bf7fa","repo":"dotnet/machinelearning","slug":"path-has-invalid-image-file-extension","errorCode":null,"errorMessage":"Path has invalid image file extension.","messagePattern":"Path has invalid image file extension\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.ImageAnalytics/MLImage.cs","lineNumber":240,"sourceCode":"                ThrowInvalidOperationExceptionIfDisposed();\n                Debug.Assert(_image.Info.BitsPerPixel == 32);\n                return _image.Info.BitsPerPixel;\n            }\n        }\n\n        /// <summary>\n        /// Save the current image to a file.\n        /// </summary>\n        /// <param name=\"imagePath\">The path of the file to save the image to.</param>\n        /// <remarks>The saved image encoding will be detected from the file extension.</remarks>\n        public void Save(string imagePath)\n        {\n            ThrowInvalidOperationExceptionIfDisposed();\n            string ext = Path.GetExtension(imagePath);\n\n            if (!_extensionToEncodingFormat.TryGetValue(ext, out SKEncodedImageFormat encodingFormat))\n            {\n                throw new ArgumentException($\"Path has invalid image file extension.\", nameof(imagePath));\n            }\n\n            using SKData data = _image.Encode(encodingFormat, 100);\n            if (data is null)\n            {\n                throw new ArgumentException($\"Saving image with the format '{ext}' is not supported. Try save it with `Jpeg`, `Png`, or `Webp` format.\", nameof(imagePath));\n            }\n\n            using var stream = new FileStream(imagePath, FileMode.Create, FileAccess.Write);\n            data.SaveTo(stream);\n        }\n\n        /// <summary>\n        /// Disposes the image.\n        /// </summary>\n        public void Dispose()\n        {\n            if (_image != null)","sourceCodeStart":222,"sourceCodeEnd":258,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.ImageAnalytics/MLImage.cs#L222-L258","documentation":"MLImage.Save infers the encoding format from the file extension via a lookup table (_extensionToEncodingFormat, covering Jpeg/Png/Webp). If the extension is missing or not one of the supported ones, ArgumentException is thrown.","triggerScenarios":"Calling image.Save(path) where Path.GetExtension(path) is not .jpg/.jpeg/.png/.webp — e.g. saving to '.bmp', '.tif', extensionless paths, or a path with a trailing dot.","commonSituations":"Saving to an extension from another library (BMP/TIFF), deriving paths from user input without normalizing extension case, or omitting the extension entirely.","solutions":["Use a .jpg, .png, or .webp extension in the save path","Convert the image first if another format is required","Normalize extension casing and ensure the path includes an extension"],"exampleFix":"// before\nimage.Save(Path.Combine(dir, name)); // no extension\n// after\nimage.Save(Path.Combine(dir, name + \".png\"));","handlingStrategy":"validation","validationCode":"string ext = Path.GetExtension(path).ToLowerInvariant();\nif (ext is not \".jpg\" and not \".jpeg\" and not \".png\" and not \".webp\")\n    path = Path.ChangeExtension(path, \".png\");","typeGuard":"bool IsSaveableExtension(string p) => new[]{\".jpg\",\".jpeg\",\".png\",\".webp\"}.Contains(Path.GetExtension(p ?? \"\").ToLowerInvariant());","tryCatchPattern":"try { image.Save(path); } catch (ArgumentException ex) when (ex.Message.Contains(\"invalid image file extension\")) { image.Save(Path.ChangeExtension(path, \".png\")); }","preventionTips":["Always append an explicit supported extension when building save paths","Normalize extension casing","Restrict user-provided save paths to Jpeg/Png/Webp"],"tags":["csharp","image","file-extension"],"backgroundTag":"invalid-argument-value","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}