Skip to content

Instantly share code, notes, and snippets.

@eldewall
Last active December 11, 2015 02:18
Show Gist options
  • Save eldewall/4529119 to your computer and use it in GitHub Desktop.
Save eldewall/4529119 to your computer and use it in GitHub Desktop.
public class Transformer : ITransformer {
public TransformResult Transform(data.Image original, ImageSpecification spec) {
double load, resize, rotate, create, sharpen;
byte[] imageBytes = null;
using (Bitmap source = ActionTimer.Time(() => { return Load(original); }, out load)) {
using (Bitmap resized = ActionTimer.Time(() => { return Resize(source, spec); }, out resize)) {
ActionTimer.Time(() => { Rotate(resized, original); }, out rotate);
ActionTimer.Time(() => { Sharpen(resized, spec.Sharpen); }, out sharpen);
ActionTimer.Time(() => { imageBytes = ImageTools.CreateJpeg(resized, spec.Quality); }, out create);
}
}
Timings t = new Timings(load, resize, rotate, create, sharpen);
return new TransformResult(imageBytes, t);
}
protected virtual void Sharpen(Bitmap image, int sharpen) {
ImageTools.Sharpen(image, sharpen, false);
}
protected virtual Bitmap Load(data.Image original) {
return (Bitmap)Image.FromFile(original.FullPath);
}
protected virtual Bitmap Resize(Bitmap source, ImageSpecification spec) {
return (Bitmap)ImageTools.Resize(source, Color.White, spec.Width, spec.Height, 1.0);
}
protected virtual void Rotate(Bitmap image, data.Image original) {
if (original.Angle.Value != 0) {
RotateFlipType type;
switch (original.Angle.Value) {
case 1:
type = RotateFlipType.Rotate90FlipNone;
break;
case 2:
type = RotateFlipType.Rotate180FlipNone;
break;
case 3:
type = RotateFlipType.Rotate270FlipNone;
break;
default:
throw new NotImplementedException();
}
image.RotateFlip(type);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment