public void RotationZoomIsCalculated(int imageWidth, int imageHeight, float angle, float expected) { float result = ImageMaths.ZoomAfterRotation(imageWidth, imageHeight, angle); result.Should().BeApproximately(expected, 0.01f, "because the zoom level after rotation should have been calculated"); result.Should().BePositive("because we're always zooming in so the zoom level should always be positive"); result.Should().BeGreaterOrEqualTo(1, "because the zoom should always increase the size and not reduce it"); }
/// <summary> /// Rotates the inside of an image to the given angle at the given position. /// </summary> /// <param name="image"> /// The image to rotate /// </param> /// <param name="angleInDegrees"> /// The angle in degrees. /// </param> /// <param name="keepSize"> /// Whether to keep the image dimensions. /// <para> /// If set to true, the image is zoomed to fit the bounding area. /// </para> /// <para> /// If set to false, the area is cropped to fit the rotated image. /// </para> /// </param> /// <remarks> /// Based on the Rotate effect /// </remarks> /// <returns> /// The image rotated to the given angle at the given position. /// </returns> private Bitmap RotateImage(Image image, float angleInDegrees, bool keepSize) { Size newSize = new Size(image.Width, image.Height); 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 = Math.Max(1, (int)Math.Floor(newSize.Width / zoom)); newSize.Height = Math.Max(1, (int)Math.Floor(newSize.Height / zoom)); } // Center of the image float rotateAtX = Math.Abs(image.Width / 2); float rotateAtY = Math.Abs(image.Height / 2); // Create a new empty bitmap to hold rotated image Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format32bppPArgb); newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); // Make a graphics object from the empty bitmap using (Graphics graphics = Graphics.FromImage(newImage)) { // Reduce the jagged edge. graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; graphics.CompositingQuality = CompositingQuality.HighQuality; if (keepSize) { // Put the rotation point in the "center" of the image graphics.TranslateTransform(rotateAtX, rotateAtY); // Rotate the image graphics.RotateTransform(angleInDegrees); // Zooms the image to fit the area graphics.ScaleTransform(zoom, zoom); // Move the image back graphics.TranslateTransform(-rotateAtX, -rotateAtY); // Draw passed in image onto graphics object graphics.DrawImage(image, new PointF(0, 0)); } else { float previousX = rotateAtX; float previousY = rotateAtY; // 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); // Draw passed in image onto graphics object graphics.DrawImage(image, new PointF(-previousX, -previousY)); } } image.Dispose(); return(newImage); }
/// <summary> /// Rotates the inside of an image to the given angle at the given position. /// </summary> /// <param name="image"> /// The image to rotate /// </param> /// <param name="angleInDegrees"> /// The angle in degrees. /// </param> /// <param name="keepSize"> /// Whether to keep the image dimensions. /// <para> /// If set to true, the image is zoomed to fit the bounding area. /// </para> /// <para> /// If set to false, the area is cropped to fit the rotated image. /// </para> /// </param> /// <remarks> /// Based on the Rotate effect /// </remarks> /// <returns> /// The image rotated to the given angle at the given position. /// </returns> private Bitmap RotateImage(Image image, float angleInDegrees, bool keepSize) { var newSize = new Size(image.Width, image.Height); var zoom = ImageMaths.ZoomAfterRotation(image.Width, image.Height, angleInDegrees); // if we don't keep the image dimensions, calculate the new ones if (!keepSize) { newSize.Width = Math.Max(1, (int)Math.Floor(newSize.Width / zoom)); newSize.Height = Math.Max(1, (int)Math.Floor(newSize.Height / zoom)); } // Center of the image float rotateAtX = Math.Abs(image.Width / 2); float rotateAtY = Math.Abs(image.Height / 2); // Create a new empty bitmap to hold rotated image var newImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format32bppPArgb); newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); // Make a graphics object from the empty bitmap using (var graphics = Graphics.FromImage(newImage)) { GraphicsHelper.SetGraphicsOptions(graphics); if (keepSize) { // Put the rotation point in the "center" of the image graphics.TranslateTransform(rotateAtX, rotateAtY); // Rotate the image graphics.RotateTransform(angleInDegrees); // Zooms the image to fit the area graphics.ScaleTransform(zoom, zoom); // Move the image back graphics.TranslateTransform(-rotateAtX, -rotateAtY); // Draw passed in image onto graphics object graphics.DrawImage(image, new PointF(0, 0)); } else { var previousX = rotateAtX; var previousY = rotateAtY; // 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); // Draw passed in image onto graphics object graphics.DrawImage(image, new PointF(-previousX, -previousY)); } } image.Dispose(); return(newImage); }