diff --git a/src/ImageProcessor.Playground/Program.cs b/src/ImageProcessor.Playground/Program.cs index ac6765f9c..6c8210e43 100644 --- a/src/ImageProcessor.Playground/Program.cs +++ b/src/ImageProcessor.Playground/Program.cs @@ -52,7 +52,7 @@ namespace ImageProcessor.PlayGround // Image mask = Image.FromFile(Path.Combine(resolvedPath, "mask.png")); // Image overlay = Image.FromFile(Path.Combine(resolvedPath, "imageprocessor.128.png")); //FileInfo fileInfo = new FileInfo(Path.Combine(resolvedPath, "2008.jpg")); - //FileInfo fileInfo = new FileInfo(Path.Combine(resolvedPath, "new-york.jpg")); + //FileInfo fileInfo = new FileInfo(Path.Combine(resolvedPath, "stretched.jpg")); //FileInfo fileInfo = new FileInfo(Path.Combine(resolvedPath, "mountain.jpg")); //FileInfo fileInfo = new FileInfo(Path.Combine(resolvedPath, "Arc-de-Triomphe-France.jpg")); //FileInfo fileInfo = new FileInfo(Path.Combine(resolvedPath, "Martin-Schoeller-Jack-Nicholson-Portrait.jpeg")); @@ -110,19 +110,19 @@ namespace ImageProcessor.PlayGround //.DetectEdges(new LaplacianOfGaussianEdgeFilter()) //.EntropyCrop() //.Halftone(true) - .RotateBounded(5, false) + .RotateBounded(150, false) + //.Rotate(140) //.Filter(MatrixFilters.Invert) //.Contrast(50) //.Filter(MatrixFilters.Comic) //.Flip() //.Filter(MatrixFilters.HiSatch) //.Pixelate(8) - //.Rotate(45) //.GaussianSharpen(10) //.Format(new PngFormat() { IsIndexed = true }) - //.Format(new PngFormat() { IsIndexed = true }) + //.Format(new PngFormat() ) .Save(Path.GetFullPath(Path.Combine(Path.GetDirectoryName(path), @"..\..\images\output", fileInfo.Name))); - //.Save(Path.GetFullPath(Path.Combine(Path.GetDirectoryName(path), @"..\..\images\output", Path.GetFileNameWithoutExtension(fileInfo.Name) + ".png"))); + //.Save(Path.GetFullPath(Path.Combine(Path.GetDirectoryName(path), @"..\..\images\output", Path.GetFileNameWithoutExtension(fileInfo.Name) + ".png"))); stopwatch.Stop(); } diff --git a/src/ImageProcessor/Imaging/Helpers/ImageMaths.cs b/src/ImageProcessor/Imaging/Helpers/ImageMaths.cs index f8ebc5d90..050c5e829 100644 --- a/src/ImageProcessor/Imaging/Helpers/ImageMaths.cs +++ b/src/ImageProcessor/Imaging/Helpers/ImageMaths.cs @@ -113,6 +113,7 @@ namespace ImageProcessor.Imaging.Helpers /// The new size of the image public static Rectangle GetBoundingRotatedRectangle(int width, int height, float angleInDegrees) { + // Check first clockwise. double radians = DegreesToRadians(angleInDegrees); double radiansSin = Math.Sin(radians); double radiansCos = Math.Cos(radians); @@ -316,14 +317,8 @@ namespace ImageProcessor.Imaging.Helpers /// The zoom needed public static float ZoomAfterRotation(int imageWidth, int imageHeight, float angleInDegrees) { - double radians = Math.Abs(DegreesToRadians(angleInDegrees)); - double radiansSin = Math.Sin(radians); - double radiansCos = Math.Cos(radians); - - double widthRotated = (imageWidth * radiansCos) + (imageHeight * radiansSin); - double heightRotated = (imageWidth * radiansSin) + (imageHeight * radiansCos); - - return (float)Math.Max(widthRotated / imageWidth, heightRotated / imageHeight); + Rectangle rectangle = GetBoundingRotatedRectangle(imageWidth, imageHeight, angleInDegrees); + return Math.Max((float)rectangle.Width / imageWidth, (float)rectangle.Height / imageHeight); } } } \ No newline at end of file diff --git a/src/ImageProcessor/Processors/RotateBounded.cs b/src/ImageProcessor/Processors/RotateBounded.cs index 20d3a7b2c..a1180e910 100644 --- a/src/ImageProcessor/Processors/RotateBounded.cs +++ b/src/ImageProcessor/Processors/RotateBounded.cs @@ -15,6 +15,7 @@ namespace ImageProcessor.Processors using System.Drawing; using System.Drawing.Drawing2D; using ImageProcessor.Common.Exceptions; + using ImageProcessor.Imaging.Helpers; /// /// Encapsulates the methods to rotate an image without expanding the canvas. @@ -98,13 +99,13 @@ namespace ImageProcessor.Processors { Size newSize = new Size(image.Width, image.Height); - float zoom = Imaging.Helpers.ImageMaths.ZoomAfterRotation(image.Width, image.Height, angleInDegrees); + float zoom = ImageMaths.ZoomAfterRotation(image.Width, image.Height, angleInDegrees); // if we don't keep the image dimensions, calculate the new ones if (!keepSize) { - newSize.Width = (int)(newSize.Width / zoom); - newSize.Height = (int)(newSize.Height / zoom); + newSize.Width = (int)Math.Floor(newSize.Width / zoom); + newSize.Height = (int)Math.Floor(newSize.Height / zoom); } // Center of the image @@ -143,22 +144,22 @@ namespace ImageProcessor.Processors } else { - // Calculate the difference between the center of the original image and the center - // of the new image. - int x = (image.Width - newSize.Width) / 2; - int y = (image.Height - newSize.Height) / 2; + float previousX = rotateAtX; + float previousY = rotateAtY; - // Put the rotation point in the "center" of the old image - graphics.TranslateTransform(rotateAtX - x, rotateAtY - y); + // Calculate the difference between the center of the original image + // and the center of the new image. + rotateAtX = Math.Abs(newImage.Width / 2); + rotateAtY = Math.Abs(newImage.Height / 2); + + // Put the rotation point in the "center" of the image + graphics.TranslateTransform(rotateAtX, rotateAtY); // Rotate the image graphics.RotateTransform(angleInDegrees); - // Move the image back - graphics.TranslateTransform(-(rotateAtX - x), -(rotateAtY - y)); - // Draw passed in image onto graphics object - graphics.DrawImage(image, new PointF(-x, -y)); + graphics.DrawImage(image, new PointF(-previousX, -previousY)); } }