Open-XML-SDK feature Proposal: method to retrieve ImagePartType from file extension

Description

In working through some abstractions for working with images in wordprocessing documents, I wrote a method for getting the ImagePartType based on the provided extension. I'm trying to see the utility of incorporating this into the API, and if so, where I might incorporate it?

// proposed method
static ImagePartType GetImagePartType(string ext) =>
    ext.ToLower() switch
    {
        ".bmp"  => ImagePartType.Bmp,
        ".emf"  => ImagePartType.Emf,
        ".ico"  => ImagePartType.Icon,
        ".jpg"  => ImagePartType.Jpeg,
        ".jpeg" => ImagePartType.Jpeg,
        ".pcx"  => ImagePartType.Pcx,
        ".png"  => ImagePartType.Png,
        ".svg"  => ImagePartType.Svg,
        ".tiff" => ImagePartType.Tiff,
        ".wmf"  => ImagePartType.Wmf,
        _ => throw new NotSupportedException($"{ext} is not supported")
    };

// example usage
static ImagePart LoadImageIntoDoc(MainDocumentPart main, FileInfo file)
{
    var imagePart = main.AddImagePart(GetImagePartType(file.Extension));
    // remaining method excluded for brevity
}
Asked Jan 10 '22 18:01
avatar JaimeStill
JaimeStill

2 Answer:

@rmboggs capabilities similar to this (but inverse) exist in an ImagePartTypeInfo class, but it is not part of the public facing API.

1
Answered Nov 28 '21 at 18:33
avatar  of JaimeStill
JaimeStill

@JaimeStill That looks very doable, please feel free to submit a PR and we can iterate on it if needed.

1
Answered Nov 29 '21 at 23:15
avatar  of tomjebo
tomjebo