Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Reference GDI+ Resizer
// Copyright (c) Clinton Ingram
// This code is licensed under the WTFPL http://www.wtfpl.net/
// If you use any significant portion of this code, attribution is appreciated but by no means required
using System;
using System.IO;
using System.Linq;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using FileFormat = PhotoSauce.MagicScaler.ProcessImageSettings.FileFormat;
namespace PhotoSauce.MagicScaler
{
public static class GdiImageProcessor
{
private const int exifOrientationID = 274;
private static readonly ImageCodecInfo jpegCodec = ImageCodecInfo.GetImageEncoders().First(c => c.FormatID == ImageFormat.Jpeg.Guid);
private static readonly ImageCodecInfo tiffCodec = ImageCodecInfo.GetImageEncoders().First(c => c.FormatID == ImageFormat.Tiff.Guid);
public static void ExifRotate(this Image img)
{
if (!img.PropertyIdList.Contains(exifOrientationID))
return;
var prop = img.GetPropertyItem(exifOrientationID);
int val = BitConverter.ToUInt16(prop.Value, 0);
var rot = RotateFlipType.RotateNoneFlipNone;
if (val == 3 || val == 4)
rot = RotateFlipType.Rotate180FlipNone;
else if (val == 5 || val == 6)
rot = RotateFlipType.Rotate90FlipNone;
else if (val == 7 || val == 8)
rot = RotateFlipType.Rotate270FlipNone;
if (val == 2 || val == 4 || val == 5 || val == 7)
rot |= RotateFlipType.RotateNoneFlipX;
if (rot != RotateFlipType.RotateNoneFlipNone)
img.RotateFlip(rot);
}
public static void ProcessImage(string imgPath, Stream ostm, ProcessImageSettings s)
{
using (var fs = new FileStream(imgPath, FileMode.Open, FileAccess.Read, FileShare.Read))
ProcessImage(fs, ostm, s);
}
public static void ProcessImage(byte[] img, Stream ostm, ProcessImageSettings s)
{
using (var ms = new MemoryStream(img, false))
ProcessImage(ms, ostm, s);
}
public static void ProcessImage(Stream istm, Stream ostm, ProcessImageSettings s)
{
using (var img = Image.FromStream(istm, true, false))
{
if (s.FrameIndex > 0)
{
var fd = img.RawFormat.Guid == ImageFormat.Gif.Guid ? FrameDimension.Time : FrameDimension.Page;
if (img.GetFrameCount(fd) > s.FrameIndex)
img.SelectActiveFrame(fd, s.FrameIndex);
else
throw new ArgumentException("Invalid Frame Index");
}
img.ExifRotate();
s.Fixup(img.Width, img.Height);
bool alpha = ((ImageFlags)img.Flags & ImageFlags.HasAlpha) == ImageFlags.HasAlpha;
using (var att = new ImageAttributes())
using (var bmp = new Bitmap(s.Width, s.Height, alpha ? PixelFormat.Format32bppArgb : PixelFormat.Format24bppRgb))
using (var gfx = Graphics.FromImage(bmp))
{
att.SetWrapMode(WrapMode.TileFlipXY);
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
gfx.PixelOffsetMode = PixelOffsetMode.Half;
gfx.CompositingMode = CompositingMode.SourceCopy;
if (alpha && s.MatteColor != Color.Empty)
{
gfx.Clear(s.MatteColor);
gfx.CompositingMode = CompositingMode.SourceOver;
gfx.CompositingQuality = CompositingQuality.GammaCorrected;
}
gfx.DrawImage(img, Rectangle.FromLTRB(0, 0, s.Width, s.Height), s.Crop.X, s.Crop.Y, s.Crop.Width, s.Crop.Height, GraphicsUnit.Pixel, att);
switch (s.SaveFormat)
{
case FileFormat.Bmp:
bmp.Save(ostm, ImageFormat.Bmp);
break;
case FileFormat.Tiff:
using (var encoderParams = new EncoderParameters(1))
using (var param = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionNone))
{
encoderParams.Param[0] = param;
bmp.Save(ostm, tiffCodec, encoderParams);
}
break;
case FileFormat.Jpeg:
using (var encoderParams = new EncoderParameters(1))
using (var param = new EncoderParameter(Encoder.Quality, (long)s.JpegQuality))
{
encoderParams.Param[0] = param;
bmp.Save(ostm, jpegCodec, encoderParams);
}
break;
default:
if (s.IndexedColor)
bmp.Save(ostm, ImageFormat.Gif);
else
bmp.Save(ostm, ImageFormat.Png);
break;
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment