ImageFormatExtensions.cs 2.0 KB

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