ImageProcessor.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.Drawing.Imaging;
  5. using System.IO;
  6. using MediaBrowser.Common.Drawing;
  7. namespace MediaBrowser.Api
  8. {
  9. public static class ImageProcessor
  10. {
  11. /// <summary>
  12. /// Resizes an image from a source stream and saves the result to an output stream
  13. /// </summary>
  14. /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
  15. /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
  16. /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
  17. /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
  18. /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
  19. public static void ProcessImage(Stream sourceImageStream, Stream toStream, int? width, int? height, int? maxWidth, int? maxHeight, int? quality)
  20. {
  21. Image originalImage = Image.FromStream(sourceImageStream);
  22. Size newSize = DrawingUtils.Resize(originalImage.Size, width, height, maxWidth, maxHeight);
  23. Bitmap thumbnail;
  24. // Graphics.FromImage will throw an exception if the PixelFormat is Indexed, so we need to handle that here
  25. if (originalImage.PixelFormat.HasFlag(PixelFormat.Indexed))
  26. {
  27. thumbnail = new Bitmap(originalImage, newSize.Width, newSize.Height);
  28. }
  29. else
  30. {
  31. thumbnail = new Bitmap(newSize.Width, newSize.Height, originalImage.PixelFormat);
  32. }
  33. thumbnail.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution);
  34. Graphics thumbnailGraph = Graphics.FromImage(thumbnail);
  35. thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
  36. thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
  37. thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
  38. thumbnailGraph.PixelOffsetMode = PixelOffsetMode.HighQuality;
  39. thumbnailGraph.CompositingMode = CompositingMode.SourceOver;
  40. thumbnailGraph.DrawImage(originalImage, 0, 0, newSize.Width, newSize.Height);
  41. Write(originalImage, thumbnail, toStream, quality);
  42. thumbnailGraph.Dispose();
  43. thumbnail.Dispose();
  44. originalImage.Dispose();
  45. }
  46. private static void Write(Image originalImage, Image newImage, Stream toStream, int? quality)
  47. {
  48. // Use special save methods for jpeg and png that will result in a much higher quality image
  49. // All other formats use the generic Image.Save
  50. if (ImageFormat.Jpeg.Equals(originalImage.RawFormat))
  51. {
  52. SaveJpeg(newImage, toStream, quality);
  53. }
  54. else if (ImageFormat.Png.Equals(originalImage.RawFormat))
  55. {
  56. newImage.Save(toStream, ImageFormat.Png);
  57. }
  58. else
  59. {
  60. newImage.Save(toStream, originalImage.RawFormat);
  61. }
  62. }
  63. private static void SaveJpeg(Image newImage, Stream target, int? quality)
  64. {
  65. if (!quality.HasValue)
  66. {
  67. quality = 90;
  68. }
  69. using (var encoderParameters = new EncoderParameters(1))
  70. {
  71. encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, quality.Value);
  72. newImage.Save(target, GetImageCodeInfo("image/jpeg"), encoderParameters);
  73. }
  74. }
  75. private static ImageCodecInfo GetImageCodeInfo(string mimeType)
  76. {
  77. ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders();
  78. for (int i = 0; i < info.Length; i++)
  79. {
  80. ImageCodecInfo ici = info[i];
  81. if (ici.MimeType.Equals(mimeType, StringComparison.OrdinalIgnoreCase))
  82. {
  83. return ici;
  84. }
  85. }
  86. return info[1];
  87. }
  88. }
  89. }