iOfficeAI/OfficeCLI · error · ArgumentException

Unsupported image format: .{ext}. Supported: png, jpg, gif,

Error message

Unsupported image format: .{ext}. Supported: png, jpg, gif, bmp, tiff, emf, wmf, svg

What it means

Thrown by ImageSource.ExtensionToContentType when a file extension does not map to any supported image format. The method maps extensions to OpenXmlPartType values, and the supported set is: png, jpg/jpeg, gif, bmp, tif/tiff, emf, wmf, svg. Any other extension (webp, heic, avif, ico, etc.) is rejected because Office's OOXML image part types do not have a representation for them.

Source

Thrown at src/officecli/Core/ImageSource.cs:62

    /// <summary>
    /// Determine content type string from a file extension (with or without dot).
    /// Returns a value usable with AddImagePart().
    /// </summary>
    public static PartTypeInfo ExtensionToContentType(string extension)
    {
        var ext = extension.TrimStart('.').ToLowerInvariant();
        return ext switch
        {
            "png" => ImagePartType.Png,
            "jpg" or "jpeg" => ImagePartType.Jpeg,
            "gif" => ImagePartType.Gif,
            "bmp" => ImagePartType.Bmp,
            "tif" or "tiff" => ImagePartType.Tiff,
            "emf" => ImagePartType.Emf,
            "wmf" => ImagePartType.Wmf,
            "svg" => ImagePartType.Svg,
            _ => throw new ArgumentException($"Unsupported image format: .{ext}. Supported: png, jpg, gif, bmp, tiff, emf, wmf, svg")
        };
    }

    private static (Stream, PartTypeInfo) ResolveFile(string path)
    {
        if (!File.Exists(path))
            throw new FileNotFoundException($"Image file not found: {path}");

        var contentType = ExtensionToContentType(Path.GetExtension(path));
        var ext = Path.GetExtension(path).TrimStart('.').ToLowerInvariant();

        // Magic-byte validation for raster formats. SVG (XML) / EMF / WMF are
        // intentionally skipped: SVG has no fixed magic, EMF/WMF have weaker
        // headers and TrySniffContentType doesn't cover them. Only validate
        // formats whose first 4 bytes are stable (png/jpg/gif/bmp/tiff).
        var rasterExts = new[] { "png", "jpg", "jpeg", "gif", "bmp", "tif", "tiff" };
        if (rasterExts.Contains(ext))
        {

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Convert the image to a supported format (png, jpg, gif, bmp, tiff, emf, wmf, or svg) before embedding.
  2. Rename the file to use a recognized extension if the format is actually supported but the extension is non-standard.
  3. For remote URLs, ensure the server sends a Content-Type header matching a supported MIME type so it is resolved from the header instead of the URL extension.

Example fix

// before — unsupported format
add image src='/tmp/photo.webp' path='/body'

// after — convert to png first
// (convert photo.webp photo.png using imagemagick or similar)
add image src='/tmp/photo.png' path='/body'
Defensive patterns

Strategy: validation

Validate before calling

// Pre-check the extension is supported
private static readonly HashSet<string> SupportedImageExts = new()
{ "png", "jpg", "jpeg", "gif", "bmp", "tif", "tiff", "emf", "wmf", "svg", StringComparer.OrdinalIgnoreCase };

string ext = Path.GetExtension(path)?.TrimStart('.').ToLowerInvariant() ?? "";
if (!SupportedImageExts.Contains(ext))
{
    Console.Error.WriteLine($"Unsupported format .{ext}. Convert to png/jpg/gif/bmp/tiff/emf/wmf/svg.");
    return;
}

Try / catch

try
{
    var (stream, contentType) = ImageSource.Resolve(source);
}
catch (ArgumentException ex) when (ex.Message.Contains("Unsupported image format"))
{
    // Convert image to png, then retry with the converted file
}

Prevention

When it happens

Trigger: Calling ImageSource.Resolve with a file path whose extension is not in the switch table (e.g. 'photo.webp', 'icon.ico'). Calling ExtensionToContentType(".avif") directly. This also fires indirectly when ResolveUrl falls back to URL-path extension extraction for a remote image whose URL ends in an unsupported extension.

Common situations: Using a modern web image format (webp, avif, heic) that Office does not natively support. A file with a double extension or unusual casing that confuses the path parser. A URL that ends in a query string or has no recognizable extension, causing the fallback to extract an unexpected value.

Related errors


AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13). Data as JSON: /api/errors/259cae7b25ebeccc. Report an issue: GitHub.