ImageFormatExtensions.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. /// <summary>
  26. /// Returns the correct extension for this <see cref="ImageFormat" />.
  27. /// </summary>
  28. /// <param name="format">This <see cref="ImageFormat" />.</param>
  29. /// <exception cref="InvalidEnumArgumentException">The <paramref name="format"/> is an invalid enumeration value.</exception>
  30. /// <returns>The correct extension for this <see cref="ImageFormat" />.</returns>
  31. public static string GetExtension(this ImageFormat format)
  32. => format switch
  33. {
  34. ImageFormat.Bmp => ".bmp",
  35. ImageFormat.Gif => ".gif",
  36. ImageFormat.Jpg => ".jpg",
  37. ImageFormat.Png => ".png",
  38. ImageFormat.Webp => ".webp",
  39. _ => throw new InvalidEnumArgumentException(nameof(format), (int)format, typeof(ImageFormat))
  40. };
  41. }