From f025a8ec208d20499e2b34a0ca770e814bef39b5 Mon Sep 17 00:00:00 2001 From: Thomas Broust Date: Wed, 25 Feb 2015 09:44:00 +0100 Subject: [PATCH] Rotates the image inside the area, but leaves blank spaces Former-commit-id: 42d309ceff7eba17b70e66d0a77577c0871ba0c7 Former-commit-id: 70757775a1babd71ff44f1b8a4f8132d0ee467e7 --- .../ImageFactoryUnitTests.cs | 3 ++ .../ImageProcessor.UnitTests.csproj | 1 - .../Processors/RotateInsideUnitTests.cs | 16 -------- src/ImageProcessor/Processors/RotateInside.cs | 41 +++++++------------ 4 files changed, 18 insertions(+), 43 deletions(-) delete mode 100644 src/ImageProcessor.UnitTests/Processors/RotateInsideUnitTests.cs diff --git a/src/ImageProcessor.UnitTests/ImageFactoryUnitTests.cs b/src/ImageProcessor.UnitTests/ImageFactoryUnitTests.cs index 870f22855..1adb0b369 100644 --- a/src/ImageProcessor.UnitTests/ImageFactoryUnitTests.cs +++ b/src/ImageProcessor.UnitTests/ImageFactoryUnitTests.cs @@ -469,6 +469,7 @@ namespace ImageProcessor.UnitTests [Test] public void ImageIsRotatedInside() { + int i = 0; foreach (ImageFactory imageFactory in this.ListInputImages()) { Image original = (Image)imageFactory.Image.Clone(); @@ -476,6 +477,8 @@ namespace ImageProcessor.UnitTests imageFactory.Image.Width.Should().Be(original.Width, "because the rotated image dimensions should not have changed"); imageFactory.Image.Height.Should().Be(original.Height, "because the rotated image dimensions should not have changed"); + + imageFactory.Format(new ImageProcessor.Imaging.Formats.JpegFormat()).Save("./output/rotateinside-" + i++.ToString() + ".jpg"); } } diff --git a/src/ImageProcessor.UnitTests/ImageProcessor.UnitTests.csproj b/src/ImageProcessor.UnitTests/ImageProcessor.UnitTests.csproj index b47e4b5bc..54ca14e2c 100644 --- a/src/ImageProcessor.UnitTests/ImageProcessor.UnitTests.csproj +++ b/src/ImageProcessor.UnitTests/ImageProcessor.UnitTests.csproj @@ -65,7 +65,6 @@ - diff --git a/src/ImageProcessor.UnitTests/Processors/RotateInsideUnitTests.cs b/src/ImageProcessor.UnitTests/Processors/RotateInsideUnitTests.cs deleted file mode 100644 index 36fe5fdcc..000000000 --- a/src/ImageProcessor.UnitTests/Processors/RotateInsideUnitTests.cs +++ /dev/null @@ -1,16 +0,0 @@ -namespace ImageProcessor.UnitTests.Processors -{ - using System.Collections.Generic; - using ImageProcessor.Processors; - using NUnit.Framework; - - public class RotateInsideUnitTests - { - [Test] - [TestCase(100, 100, 15, 150)] - public void ZoomIsCalculated(int width, int height, float angle, float expected) - { - - } - } -} \ No newline at end of file diff --git a/src/ImageProcessor/Processors/RotateInside.cs b/src/ImageProcessor/Processors/RotateInside.cs index f958db8d7..05675be31 100644 --- a/src/ImageProcessor/Processors/RotateInside.cs +++ b/src/ImageProcessor/Processors/RotateInside.cs @@ -74,32 +74,21 @@ namespace ImageProcessor.Processors return image; } + /// + /// Rotates the inside of an image to the given angle at the given position. + /// + /// The image to rotate + /// The horizontal pixel coordinate at which to rotate the image. + /// The vertical pixel coordinate at which to rotate the image. + /// The angle in degrees at which to rotate the image. + /// The image rotated to the given angle at the given position. + /// + /// Based on + /// private Bitmap RotateImage(Image image, float rotateAtX, float rotateAtY, float angle) { - double widthAsDouble = image.Width; - double heightAsDouble = image.Height; - - double radians = angle * Math.PI / 180d; - double radiansSin = Math.Sin(radians); - double radiansCos = Math.Cos(radians); - double width1 = (heightAsDouble * radiansSin) + (widthAsDouble * radiansCos); - double height1 = (widthAsDouble * radiansSin) + (heightAsDouble * radiansCos); - - // Find dimensions in the other direction - radiansSin = Math.Sin(-radians); - radiansCos = Math.Cos(-radians); - double width2 = (heightAsDouble * radiansSin) + (widthAsDouble * radiansCos); - double height2 = (widthAsDouble * radiansSin) + (heightAsDouble * radiansCos); - - // Get the external vertex for the rotation - int width = Convert.ToInt32(Math.Max(Math.Abs(width1), Math.Abs(width2))); - int height = Convert.ToInt32(Math.Max(Math.Abs(height1), Math.Abs(height2))); - - int x = (width - image.Width) / 2; - int y = (height - image.Height) / 2; - // Create a new empty bitmap to hold rotated image - Bitmap newImage = new Bitmap(width, height); + Bitmap newImage = new Bitmap(image.Width, image.Height); newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); // Make a graphics object from the empty bitmap @@ -112,16 +101,16 @@ namespace ImageProcessor.Processors graphics.CompositingQuality = CompositingQuality.HighQuality; // Put the rotation point in the "center" of the image - graphics.TranslateTransform(rotateAtX + x, rotateAtY + y); + graphics.TranslateTransform(rotateAtX, rotateAtY); // Rotate the image graphics.RotateTransform(angle); // Move the image back - graphics.TranslateTransform(-rotateAtX - x, -rotateAtY - y); + graphics.TranslateTransform(-rotateAtX * 2, -rotateAtY * 2); // Draw passed in image onto graphics object - graphics.DrawImage(image, new PointF(x, y)); + graphics.DrawImage(image, new PointF(rotateAtX, rotateAtY)); } return newImage;