microsoft/semantic-kernel · error · NotSupportedException

Unsupported image format.

Error message

Unsupported image format.

What it means

GetMimeType maps a file extension to a MIME type and throws NotSupportedException for any extension outside jpg/jpeg/png/gif/bmp/tiff/ico/svg. This MIME value is sent to the HuggingFace image-to-text service, so unsupported types can't be submitted. The throw is the catch-all `_` arm of the switch expression.

Source

Thrown at dotnet/samples/Demos/HuggingFaceImageToText/FormMain.cs:192

    }

    /// <summary>
    /// Gets the MIME type of the specific image file extension
    /// </summary>
    /// <param name="fileName">The file name with extension</param>
    /// <returns>The MIME type of the specific image file extension</returns>
    private static string GetMimeType(string fileName)
    {
        return Path.GetExtension(fileName) switch
        {
            ".jpg" or ".jpeg" => "image/jpeg",
            ".png" => "image/png",
            ".gif" => "image/gif",
            ".bmp" => "image/bmp",
            ".tiff" => "image/tiff",
            ".ico" => "image/x-icon",
            ".svg" => "image/svg+xml",
            _ => throw new NotSupportedException("Unsupported image format.")
        };
    }

    private static ImageFormat GetImageFormat(string fileName)
    {
        return Path.GetExtension(fileName) switch
        {
            ".jpg" or ".jpeg" => ImageFormat.Jpeg,
            ".png" => ImageFormat.Png,
            ".gif" => ImageFormat.Gif,
            ".bmp" => ImageFormat.Bmp,
            ".tiff" => ImageFormat.Tiff,
            ".ico" => ImageFormat.Icon,
            ".svg" => ImageFormat.MemoryBmp,
            _ => throw new NotSupportedException("Unsupported image format.")
        };
    }

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Convert the image to one of the supported formats (png/jpg) before loading.
  2. Add the missing extension(s) and their MIME types to the switch if the backend accepts them.
  3. Filter the file picker to allowed extensions so unsupported files can't be selected.

Example fix

// before
_ => throw new NotSupportedException("Unsupported image format.")

// after
".webp" => "image/webp",
".avif" => "image/avif",
_ => throw new NotSupportedException($"Unsupported image format '{Path.GetExtension(fileName)}'.")
Defensive patterns

Strategy: validation

Validate before calling

static readonly HashSet<string> s_supported = new(StringComparer.OrdinalIgnoreCase)
    { ".jpg", ".jpeg", ".png", ".gif", ".bmp", ".tiff", ".ico", ".svg" };
if (!s_supported.Contains(Path.GetExtension(fileName)))
    throw new NotSupportedException($"Unsupported image format '{Path.GetExtension(fileName)}'.");

Type guard

static bool IsSupportedImage(string fileName) =>
    s_supported.Contains(Path.GetExtension(fileName));

Prevention

When it happens

Trigger: Path.GetExtension(fileName) returns something not in the supported set — e.g. .webp, .heic, .avif, or an empty/no extension.

Common situations: User selects a modern format (webp/avif/heic) the sample doesn't whitelist, or a file with no extension is dropped.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/42b0c2d4cb1206c6. Report an issue: GitHub.