Search code examples
c#.netsystem.drawing.imaging

get extension from System.Drawing.Imaging.ImageFormat (C#)


Is it possible to get the extension, for any given System.Drawing.Imaging.ImageFormat? (C#)

Example:

System.Drawing.Imaging.ImageFormat.Tiff -> .tif
System.Drawing.Imaging.ImageFormat.Jpeg -> .jpg
...

This can easily be done as a lookup table, but wanted to know if there is anything natively in .Net.


Solution

  • mayby this is what you are looking for?

        public static string GetFilenameExtension(ImageFormat format)
        {
            return ImageCodecInfo.GetImageEncoders().FirstOrDefault(x => x.FormatID == format.Guid).FilenameExtension;
        }