{"record":{"id":"259cae7b25ebeccc","repo":"iOfficeAI/OfficeCLI","slug":"unsupported-image-format-ext-supported-png","errorCode":null,"errorMessage":"Unsupported image format: .{ext}. Supported: png, jpg, gif, bmp, tiff, emf, wmf, svg","messagePattern":"Unsupported image format: \\.(.+?)\\. Supported: png, jpg, gif, bmp, tiff, emf, wmf, svg","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Core/ImageSource.cs","lineNumber":62,"sourceCode":"\n    /// <summary>\n    /// Determine content type string from a file extension (with or without dot).\n    /// Returns a value usable with AddImagePart().\n    /// </summary>\n    public static PartTypeInfo ExtensionToContentType(string extension)\n    {\n        var ext = extension.TrimStart('.').ToLowerInvariant();\n        return ext switch\n        {\n            \"png\" => ImagePartType.Png,\n            \"jpg\" or \"jpeg\" => ImagePartType.Jpeg,\n            \"gif\" => ImagePartType.Gif,\n            \"bmp\" => ImagePartType.Bmp,\n            \"tif\" or \"tiff\" => ImagePartType.Tiff,\n            \"emf\" => ImagePartType.Emf,\n            \"wmf\" => ImagePartType.Wmf,\n            \"svg\" => ImagePartType.Svg,\n            _ => throw new ArgumentException($\"Unsupported image format: .{ext}. Supported: png, jpg, gif, bmp, tiff, emf, wmf, svg\")\n        };\n    }\n\n    private static (Stream, PartTypeInfo) ResolveFile(string path)\n    {\n        if (!File.Exists(path))\n            throw new FileNotFoundException($\"Image file not found: {path}\");\n\n        var contentType = ExtensionToContentType(Path.GetExtension(path));\n        var ext = Path.GetExtension(path).TrimStart('.').ToLowerInvariant();\n\n        // Magic-byte validation for raster formats. SVG (XML) / EMF / WMF are\n        // intentionally skipped: SVG has no fixed magic, EMF/WMF have weaker\n        // headers and TrySniffContentType doesn't cover them. Only validate\n        // formats whose first 4 bytes are stable (png/jpg/gif/bmp/tiff).\n        var rasterExts = new[] { \"png\", \"jpg\", \"jpeg\", \"gif\", \"bmp\", \"tif\", \"tiff\" };\n        if (rasterExts.Contains(ext))\n        {","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Core/ImageSource.cs#L44-L80","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Convert the image to a supported format (png, jpg, gif, bmp, tiff, emf, wmf, or svg) before embedding.","Rename the file to use a recognized extension if the format is actually supported but the extension is non-standard.","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."],"exampleFix":"// before — unsupported format\nadd image src='/tmp/photo.webp' path='/body'\n\n// after — convert to png first\n// (convert photo.webp photo.png using imagemagick or similar)\nadd image src='/tmp/photo.png' path='/body'","handlingStrategy":"validation","validationCode":"// Pre-check the extension is supported\nprivate static readonly HashSet<string> SupportedImageExts = new()\n{ \"png\", \"jpg\", \"jpeg\", \"gif\", \"bmp\", \"tif\", \"tiff\", \"emf\", \"wmf\", \"svg\", StringComparer.OrdinalIgnoreCase };\n\nstring ext = Path.GetExtension(path)?.TrimStart('.').ToLowerInvariant() ?? \"\";\nif (!SupportedImageExts.Contains(ext))\n{\n    Console.Error.WriteLine($\"Unsupported format .{ext}. Convert to png/jpg/gif/bmp/tiff/emf/wmf/svg.\");\n    return;\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    var (stream, contentType) = ImageSource.Resolve(source);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Unsupported image format\"))\n{\n    // Convert image to png, then retry with the converted file\n}","preventionTips":["Maintain a set of supported extensions and validate file paths against it before embedding.","For remote URLs, prefer URLs with standard image extensions or ensure the server sends a supported Content-Type.","Convert modern formats (webp, heic, avif) to png/jpg before embedding into Office documents."],"tags":["image","format-unsupported","extension-validation","add-command"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}