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.
I have now found 3 ways to do this, of which, the last 2 are equivalent. All are extension methods and intend to produce an extension in the form ".foo"
static class ImageFormatUtils
{
//Attempt 1: Use ImageCodecInfo.GetImageEncoders
public static string FileExtensionFromEncoder(this ImageFormat format)
{
try
{
return ImageCodecInfo.GetImageEncoders()
.First(x => x.FormatID == format.Guid)
.FilenameExtension
.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
.First()
.Trim('*')
.ToLower();
}
catch (Exception)
{
return ".IDFK";
}
}
//Attempt 2: Using ImageFormatConverter().ConvertToString()
public static string FileExtensionFromConverter(this ImageFormat format)
{
return "." + new ImageFormatConverter().ConvertToString(format).ToLower();
}
//Attempt 3: Using ImageFormat.ToString()
public static string FileExtensionFromToString(this ImageFormat format)
{
return "." + format.ToString().ToLower();
}
}
Being extension methods they can be called like:
ImageFormat format = ImageFormat.Jpeg;
var filePath = "image" + format.FileExtensionFromEncoder();
The resulting string will be:
"image.jpg"
To compare these new methods I made a short console app:
class Program
{
static void Main()
{
var formats = new[]
{
ImageFormat.Bmp, ImageFormat.Emf, ImageFormat.Exif, ImageFormat.Gif,
ImageFormat.Icon, ImageFormat.Jpeg, ImageFormat.MemoryBmp, ImageFormat.Png,
ImageFormat.Tiff, ImageFormat.Wmf
};
foreach (var format in formats)
{
Console.WriteLine("FromEncoder: '{0}', FromConverter: '{1}', FromToString: '{2}'", format.FileExtensionFromEncoder(), format.FileExtensionFromConverter(), format.FileExtensionFromToString());
}
Console.Read();
}
}
Running this results in the following output:
FromEncoder: '.bmp', FromConverter: '.bmp', FromToString: '.bmp'
FromEncoder: '.IDFK', FromConverter: '.emf', FromToString: '.emf'
FromEncoder: '.IDFK', FromConverter: '.exif', FromToString: '.exif'
FromEncoder: '.gif', FromConverter: '.gif', FromToString: '.gif'
FromEncoder: '.IDFK', FromConverter: '.icon', FromToString: '.icon'
FromEncoder: '.jpg', FromConverter: '.jpeg', FromToString: '.jpeg'
FromEncoder: '.IDFK', FromConverter: '.memorybmp', FromToString: '.memorybmp'
FromEncoder: '.png', FromConverter: '.png', FromToString: '.png'
FromEncoder: '.tif', FromConverter: '.tiff', FromToString: '.tiff'
FromEncoder: '.IDFK', FromConverter: '.wmf', FromToString: '.wmf'
You can see that the original method fails and produces a '.IDFK' on the more obscure formats while the other methods are actually just using the name of the format; ImageFormat.Jpeg, '.jpeg'; ImageFormat.MemoryBmp, '.memorybmp'; etc..
So as the original question wants '.tif' rather then '.tiff', it would seem the first method is for you. Or maybe some combination of the 2 would be ideal:
public static string FileExtensionFromEncoder(this ImageFormat format)
{
try
{
return ImageCodecInfo.GetImageEncoders()
.First(x => x.FormatID == format.Guid)
.FilenameExtension
.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
.First()
.Trim('*')
.ToLower();
}
catch (Exception)
{
return "." + format.ToString().ToLower();
}
}
mayby this is what you are looking for?
public static string GetFilenameExtension(ImageFormat format)
{
return ImageCodecInfo.GetImageEncoders().FirstOrDefault(x => x.FormatID == format.Guid).FilenameExtension;
}
Kevin Bray's answer is great. I wasn't 100% happy with relying on catching an exception in this way though, so I tweaked his solution very slightly...
public static string GetFileExtension(this ImageFormat imageFormat)
{
var extension = ImageCodecInfo.GetImageEncoders()
.Where(ie => ie.FormatID == imageFormat.Guid)
.Select(ie => ie.FilenameExtension
.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
.First()
.Trim('*')
.ToLower())
.FirstOrDefault();
return extension ?? string.Format(".{0}", imageFormat.ToString().ToLower());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With