-
-
Save eldewall/4529119 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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