2012-09-16 23:12:39 +00:00
|
|
|
|
using MediaBrowser.Common.Drawing;
|
|
|
|
|
using System;
|
2012-07-13 03:50:50 +00:00
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Drawing.Drawing2D;
|
|
|
|
|
using System.Drawing.Imaging;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api
|
|
|
|
|
{
|
|
|
|
|
public static class ImageProcessor
|
|
|
|
|
{
|
2012-08-23 18:35:44 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Resizes an image from a source stream and saves the result to an output stream
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
|
|
|
|
|
/// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
|
|
|
|
|
/// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
|
|
|
|
|
/// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
|
|
|
|
|
/// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
|
2012-08-15 13:20:29 +00:00
|
|
|
|
public static void ProcessImage(Stream sourceImageStream, Stream toStream, int? width, int? height, int? maxWidth, int? maxHeight, int? quality)
|
2012-07-13 03:50:50 +00:00
|
|
|
|
{
|
2012-08-15 13:20:29 +00:00
|
|
|
|
Image originalImage = Image.FromStream(sourceImageStream);
|
2012-07-13 03:50:50 +00:00
|
|
|
|
|
2012-08-16 02:42:37 +00:00
|
|
|
|
Size newSize = DrawingUtils.Resize(originalImage.Size, width, height, maxWidth, maxHeight);
|
2012-07-13 03:50:50 +00:00
|
|
|
|
|
2012-09-06 16:00:36 +00:00
|
|
|
|
Bitmap thumbnail;
|
|
|
|
|
|
|
|
|
|
// Graphics.FromImage will throw an exception if the PixelFormat is Indexed, so we need to handle that here
|
|
|
|
|
if (originalImage.PixelFormat.HasFlag(PixelFormat.Indexed))
|
|
|
|
|
{
|
|
|
|
|
thumbnail = new Bitmap(originalImage, newSize.Width, newSize.Height);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
thumbnail = new Bitmap(newSize.Width, newSize.Height, originalImage.PixelFormat);
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-13 03:50:50 +00:00
|
|
|
|
thumbnail.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution);
|
|
|
|
|
|
|
|
|
|
Graphics thumbnailGraph = Graphics.FromImage(thumbnail);
|
2012-09-06 16:00:36 +00:00
|
|
|
|
|
2012-07-13 03:50:50 +00:00
|
|
|
|
thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
|
|
|
|
|
thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
|
|
|
|
|
thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
|
|
|
|
thumbnailGraph.PixelOffsetMode = PixelOffsetMode.HighQuality;
|
|
|
|
|
thumbnailGraph.CompositingMode = CompositingMode.SourceOver;
|
|
|
|
|
|
2012-08-16 02:42:37 +00:00
|
|
|
|
thumbnailGraph.DrawImage(originalImage, 0, 0, newSize.Width, newSize.Height);
|
2012-07-13 03:50:50 +00:00
|
|
|
|
|
|
|
|
|
Write(originalImage, thumbnail, toStream, quality);
|
|
|
|
|
|
|
|
|
|
thumbnailGraph.Dispose();
|
|
|
|
|
thumbnail.Dispose();
|
|
|
|
|
originalImage.Dispose();
|
|
|
|
|
}
|
2012-09-06 16:00:36 +00:00
|
|
|
|
|
2012-07-13 03:50:50 +00:00
|
|
|
|
private static void Write(Image originalImage, Image newImage, Stream toStream, int? quality)
|
|
|
|
|
{
|
|
|
|
|
// Use special save methods for jpeg and png that will result in a much higher quality image
|
|
|
|
|
// All other formats use the generic Image.Save
|
|
|
|
|
if (ImageFormat.Jpeg.Equals(originalImage.RawFormat))
|
|
|
|
|
{
|
|
|
|
|
SaveJpeg(newImage, toStream, quality);
|
|
|
|
|
}
|
|
|
|
|
else if (ImageFormat.Png.Equals(originalImage.RawFormat))
|
|
|
|
|
{
|
2012-08-16 02:42:37 +00:00
|
|
|
|
newImage.Save(toStream, ImageFormat.Png);
|
2012-07-13 03:50:50 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
newImage.Save(toStream, originalImage.RawFormat);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void SaveJpeg(Image newImage, Stream target, int? quality)
|
|
|
|
|
{
|
|
|
|
|
if (!quality.HasValue)
|
|
|
|
|
{
|
|
|
|
|
quality = 90;
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-11 19:37:14 +00:00
|
|
|
|
using (var encoderParameters = new EncoderParameters(1))
|
2012-07-13 03:50:50 +00:00
|
|
|
|
{
|
|
|
|
|
encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, quality.Value);
|
|
|
|
|
newImage.Save(target, GetImageCodeInfo("image/jpeg"), encoderParameters);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static ImageCodecInfo GetImageCodeInfo(string mimeType)
|
|
|
|
|
{
|
|
|
|
|
ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < info.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
ImageCodecInfo ici = info[i];
|
|
|
|
|
if (ici.MimeType.Equals(mimeType, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return ici;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return info[1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|