ImageProcessor.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Model.Entities;
  3. using System;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. using System.Drawing.Imaging;
  7. using System.IO;
  8. using System.Linq;
  9. namespace MediaBrowser.Api.Drawing
  10. {
  11. public static class ImageProcessor
  12. {
  13. /// <summary>
  14. /// Processes an image by resizing to target dimensions
  15. /// </summary>
  16. /// <param name="entity">The entity that owns the image</param>
  17. /// <param name="imageType">The image type</param>
  18. /// <param name="imageIndex">The image index (currently only used with backdrops)</param>
  19. /// <param name="toStream">The stream to save the new image to</param>
  20. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  21. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  22. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  23. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  24. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  25. public static void ProcessImage(BaseEntity entity, ImageType imageType, int imageIndex, Stream toStream, int? width, int? height, int? maxWidth, int? maxHeight, int? quality)
  26. {
  27. Image originalImage = Image.FromFile(GetImagePath(entity, imageType, imageIndex));
  28. // Determine the output size based on incoming parameters
  29. Size newSize = DrawingUtils.Resize(originalImage.Size, width, height, maxWidth, maxHeight);
  30. Bitmap thumbnail;
  31. // Graphics.FromImage will throw an exception if the PixelFormat is Indexed, so we need to handle that here
  32. if (originalImage.PixelFormat.HasFlag(PixelFormat.Indexed))
  33. {
  34. thumbnail = new Bitmap(originalImage, newSize.Width, newSize.Height);
  35. }
  36. else
  37. {
  38. thumbnail = new Bitmap(newSize.Width, newSize.Height, originalImage.PixelFormat);
  39. }
  40. thumbnail.MakeTransparent();
  41. // Preserve the original resolution
  42. thumbnail.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution);
  43. Graphics thumbnailGraph = Graphics.FromImage(thumbnail);
  44. thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
  45. thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
  46. thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
  47. thumbnailGraph.PixelOffsetMode = PixelOffsetMode.HighQuality;
  48. thumbnailGraph.CompositingMode = CompositingMode.SourceOver;
  49. thumbnailGraph.DrawImage(originalImage, 0, 0, newSize.Width, newSize.Height);
  50. ImageFormat outputFormat = originalImage.RawFormat;
  51. // Write to the output stream
  52. SaveImage(outputFormat, thumbnail, toStream, quality);
  53. thumbnailGraph.Dispose();
  54. thumbnail.Dispose();
  55. originalImage.Dispose();
  56. }
  57. public static string GetImagePath(BaseEntity entity, ImageType imageType, int imageIndex)
  58. {
  59. var item = entity as BaseItem;
  60. if (item != null)
  61. {
  62. if (imageType == ImageType.Logo)
  63. {
  64. return item.LogoImagePath;
  65. }
  66. if (imageType == ImageType.Backdrop)
  67. {
  68. return item.BackdropImagePaths.ElementAt(imageIndex);
  69. }
  70. if (imageType == ImageType.Banner)
  71. {
  72. return item.BannerImagePath;
  73. }
  74. if (imageType == ImageType.Art)
  75. {
  76. return item.ArtImagePath;
  77. }
  78. if (imageType == ImageType.Thumbnail)
  79. {
  80. return item.ThumbnailImagePath;
  81. }
  82. }
  83. return entity.PrimaryImagePath;
  84. }
  85. public static void SaveImage(ImageFormat outputFormat, Image newImage, Stream toStream, int? quality)
  86. {
  87. // Use special save methods for jpeg and png that will result in a much higher quality image
  88. // All other formats use the generic Image.Save
  89. if (ImageFormat.Jpeg.Equals(outputFormat))
  90. {
  91. SaveJpeg(newImage, toStream, quality);
  92. }
  93. else if (ImageFormat.Png.Equals(outputFormat))
  94. {
  95. newImage.Save(toStream, ImageFormat.Png);
  96. }
  97. else
  98. {
  99. newImage.Save(toStream, outputFormat);
  100. }
  101. }
  102. public static void SaveJpeg(Image image, Stream target, int? quality)
  103. {
  104. if (!quality.HasValue)
  105. {
  106. quality = 90;
  107. }
  108. using (var encoderParameters = new EncoderParameters(1))
  109. {
  110. encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, quality.Value);
  111. image.Save(target, GetImageCodecInfo("image/jpeg"), encoderParameters);
  112. }
  113. }
  114. public static ImageCodecInfo GetImageCodecInfo(string mimeType)
  115. {
  116. ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders();
  117. for (int i = 0; i < info.Length; i++)
  118. {
  119. ImageCodecInfo ici = info[i];
  120. if (ici.MimeType.Equals(mimeType, StringComparison.OrdinalIgnoreCase))
  121. {
  122. return ici;
  123. }
  124. }
  125. return info[1];
  126. }
  127. }
  128. }