ImageFormatExtensions.cs 1.1 KB

123456789101112131415161718192021222324252627
  1. using System.ComponentModel;
  2. using System.Net.Mime;
  3. namespace MediaBrowser.Model.Drawing;
  4. /// <summary>
  5. /// Extension class for the <see cref="ImageFormat" /> enum.
  6. /// </summary>
  7. public static class ImageFormatExtensions
  8. {
  9. /// <summary>
  10. /// Returns the correct mime type for this <see cref="ImageFormat" />.
  11. /// </summary>
  12. /// <param name="format">This <see cref="ImageFormat" />.</param>
  13. /// <exception cref="InvalidEnumArgumentException">The <paramref name="format"/> is an invalid enumeration value.</exception>
  14. /// <returns>The correct mime type for this <see cref="ImageFormat" />.</returns>
  15. public static string GetMimeType(this ImageFormat format)
  16. => format switch
  17. {
  18. ImageFormat.Bmp => "image/bmp",
  19. ImageFormat.Gif => MediaTypeNames.Image.Gif,
  20. ImageFormat.Jpg => MediaTypeNames.Image.Jpeg,
  21. ImageFormat.Png => "image/png",
  22. ImageFormat.Webp => "image/webp",
  23. _ => throw new InvalidEnumArgumentException(nameof(format), (int)format, typeof(ImageFormat))
  24. };
  25. }