dotnet/machinelearning · error · ArgumentException

Saving image with the format

Error message

Saving image with the format '{ext}' is not supported. Try save it with `Jpeg`, `Png`, or `Webp` format.

What it means

After selecting an encoding format from the extension, Save calls _image.Encode(encodingFormat, 100). If SkiaSharp's encoder returns null for that format, the format is effectively unsupported by the encoder and ArgumentException is thrown.

Solutions

  1. Save as Jpeg, Png, or Webp as the message suggests
  2. Update the SkiaSharp native assets to a build that includes the needed encoder
  3. Catch ArgumentException and retry with a .png path

Example fix

// before
image.Save("out.webp"); // encoder returned null
// after
try { image.Save("out.webp"); } catch (ArgumentException) { image.Save("out.png"); }
Defensive patterns

Strategy: fallback

Validate before calling

// no pre-call API surface; prefer known-good formats
string ext = Path.GetExtension(path).ToLowerInvariant();
bool knownGood = ext is ".jpg" or ".jpeg" or ".png" or ".webp";

Try / catch

try { image.Save(path); } catch (ArgumentException ex) when (ex.Message.Contains("is not supported")) { image.Save(Path.ChangeExtension(path, ".png")); }

Prevention

When it happens

Trigger: Calling Save with a recognized extension whose SKImage encode operation returns null — typically when the SkiaSharp build lacks the encoder for that format.

Common situations: Rare encoder-availability differences across platforms/SkiaSharp versions; mostly avoided by sticking to JPEG, PNG, or WebP.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

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

        /// <summary>
        /// Save the current image to a file.
        /// </summary>
        /// <param name="imagePath">The path of the file to save the image to.</param>
        /// <remarks>The saved image encoding will be detected from the file extension.</remarks>
        public void Save(string imagePath)
        {
            ThrowInvalidOperationExceptionIfDisposed();
            string ext = Path.GetExtension(imagePath);

            if (!_extensionToEncodingFormat.TryGetValue(ext, out SKEncodedImageFormat encodingFormat))
            {
                throw new ArgumentException($"Path has invalid image file extension.", nameof(imagePath));
            }

            using SKData data = _image.Encode(encodingFormat, 100);
            if (data is null)
            {
                throw new ArgumentException($"Saving image with the format '{ext}' is not supported. Try save it with `Jpeg`, `Png`, or `Webp` format.", nameof(imagePath));
            }

            using var stream = new FileStream(imagePath, FileMode.Create, FileAccess.Write);
            data.SaveTo(stream);
        }

        /// <summary>
        /// Disposes the image.
        /// </summary>
        public void Dispose()
        {
            if (_image != null)
            {
                _image.Dispose();
                _image = null;
            }
        }

View on GitHub (pinned to 7b76e69cf9)