Beispiel #1
0
        /// <summary>
        /// Rotates an image to the given angle at the given position.
        /// </summary>
        /// <param name="image">The image to rotate</param>
        /// <param name="rotateAtX">The horizontal pixel coordinate at which to rotate the image.</param>
        /// <param name="rotateAtY">The vertical pixel coordinate at which to rotate the image.</param>
        /// <param name="angle">The angle in degrees at which to rotate the image.</param>
        /// <returns>The image rotated to the given angle at the given position.</returns>
        /// <remarks>
        /// Based on <see href="http://www.codeproject.com/Articles/58815/C-Image-PictureBox-Rotations?msg=4155374#xx4155374xx"/>
        /// </remarks>
        private Bitmap RotateImage(Image image, float rotateAtX, float rotateAtY, float angle)
        {
            var newSize = ImageMaths.GetBoundingRotatedRectangle(image.Width, image.Height, angle);

            var x = (newSize.Width - image.Width) / 2;
            var y = (newSize.Height - 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))
            {
                // Reduce the jagged edge.
                GraphicsHelper.SetGraphicsOptions(graphics);

                // Put the rotation point in the "center" of the image
                graphics.TranslateTransform(rotateAtX + x, rotateAtY + y);

                // Rotate the image
                graphics.RotateTransform(angle);

                // Move the image back
                graphics.TranslateTransform(-rotateAtX - x, -rotateAtY - y);

                // Draw passed in image onto graphics object
                graphics.DrawImage(image, new PointF(x, y));
            }

            image.Dispose();
            return(newImage);
        }
        public void BoundingRotatedRectangleIsCalculated(int width, int height, float angle, int expectedWidth, int expectedHeight)
        {
            Rectangle result = ImageMaths.GetBoundingRotatedRectangle(width, height, angle);

            result.Width.Should().Be(expectedWidth, "because the rotated width should have been calculated");
            result.Height.Should().Be(expectedHeight, "because the rotated height should have been calculated");
        }
Beispiel #3
0
            public void ThenReturnRotatedRectangleAt00And5By2Given52And180Degrees(int degrees)
            {
                // Arrange // Act
                var rectangle = ImageMaths.GetBoundingRotatedRectangle(5, 2, degrees);

                // Assert
                Assert.That(rectangle, Is.EqualTo(new Rectangle(0, 0, 5, 2)));
            }
Beispiel #4
0
            public void ThenShouldReturnRotatedRectangleAt00And7By7Given5By5AndDegrees(int degrees)
            {
                // Arrange // Act
                var rectangle = ImageMaths.GetBoundingRotatedRectangle(5, 5, degrees);

                // Assert
                Assert.That(rectangle, Is.EqualTo(new Rectangle(0, 0, 7, 7)));
            }
Beispiel #5
0
        /// <summary>
        /// Gets the bounding rectangle of the image based on the rotating angles.
        /// </summary>
        /// <param name="width">
        /// The width of the image.
        /// </param>
        /// <param name="height">
        /// The height of the image.
        /// </param>
        /// <returns>
        /// The <see cref="Rectangle"/>.
        /// </returns>
        private Rectangle GetBoundingRectangle(int width, int height)
        {
            int          maxWidth  = 0;
            int          maxHeight = 0;
            List <float> angles    = new List <float> {
                this.CyanAngle, this.MagentaAngle, this.YellowAngle, this.KeylineAngle
            };

            foreach (float angle in angles)
            {
                Size rotatedSize = ImageMaths.GetBoundingRotatedRectangle(width, height, angle).Size;
                maxWidth  = Math.Max(maxWidth, rotatedSize.Width);
                maxHeight = Math.Max(maxHeight, rotatedSize.Height);
            }

            return(new Rectangle(0, 0, maxWidth, maxHeight));
        }