// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) James South. // Licensed under the Apache License, Version 2.0. // // // Encapsulates a series of time saving extension methods to the // class. // // -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor.Extensions { #region using System.Drawing.Imaging; using System.Linq; #endregion /// /// Encapsulates a series of time saving extension methods to the class. /// public static class ImageFormatExtensions { /// /// Gets the correct mime-type for the given . /// /// The . /// The correct mime-type for the given . public static string GetMimeType(this ImageFormat imageFormat) { if (imageFormat.Equals(ImageFormat.Icon)) { return "image/x-icon"; } ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); return codecs.First(codec => codec.FormatID == imageFormat.Guid).MimeType; } /// /// Gets the name for the given . /// /// /// The to get the name for. /// /// /// The representing the name of the . /// public static string GetName(this ImageFormat format) { if (format.Guid == ImageFormat.MemoryBmp.Guid) { return "MemoryBMP"; } if (format.Guid == ImageFormat.Bmp.Guid) { return "Bmp"; } if (format.Guid == ImageFormat.Emf.Guid) { return "Emf"; } if (format.Guid == ImageFormat.Wmf.Guid) { return "Wmf"; } if (format.Guid == ImageFormat.Gif.Guid) { return "Gif"; } if (format.Guid == ImageFormat.Jpeg.Guid) { return "Jpeg"; } if (format.Guid == ImageFormat.Png.Guid) { return "Png"; } if (format.Guid == ImageFormat.Tiff.Guid) { return "Tiff"; } if (format.Guid == ImageFormat.Exif.Guid) { return "Exif"; } if (format.Guid == ImageFormat.Icon.Guid) { return "Icon"; } return "[ImageFormat: " + format.Guid + "]"; } /// /// Returns the correct file extension for the given . /// /// /// The to return the extension for. /// /// /// The original Extension. /// /// /// The correct file extension for the given . /// public static string GetFileExtension(this ImageFormat imageFormat, string originalExtension) { string name = imageFormat.GetName(); switch (name) { case "Icon": return ".ico"; case "Gif": return ".gif"; case "Bmp": return ".bmp"; case "Png": return ".png"; case "Tiff": if (!string.IsNullOrWhiteSpace(originalExtension) && originalExtension.ToUpperInvariant() == ".TIF") { return ".tif"; } return ".tiff"; case "Jpeg": if (!string.IsNullOrWhiteSpace(originalExtension) && originalExtension.ToUpperInvariant() == ".JPG") { return ".jpg"; } break; } return null; } } }