Browse Source

Rotates the image inside the area, but leaves blank spaces

Former-commit-id: 42d309ceff7eba17b70e66d0a77577c0871ba0c7
Former-commit-id: 70757775a1babd71ff44f1b8a4f8132d0ee467e7
pull/17/head
Thomas Broust 11 years ago
parent
commit
f025a8ec20
  1. 3
      src/ImageProcessor.UnitTests/ImageFactoryUnitTests.cs
  2. 1
      src/ImageProcessor.UnitTests/ImageProcessor.UnitTests.csproj
  3. 16
      src/ImageProcessor.UnitTests/Processors/RotateInsideUnitTests.cs
  4. 41
      src/ImageProcessor/Processors/RotateInside.cs

3
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");
}
}

1
src/ImageProcessor.UnitTests/ImageProcessor.UnitTests.csproj

@ -65,7 +65,6 @@
<Compile Include="Imaging\CropLayerUnitTests.cs" />
<Compile Include="Imaging\FastBitmapUnitTests.cs" />
<Compile Include="Imaging\RotationUnitTests.cs" />
<Compile Include="Processors\RotateInsideUnitTests.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>

16
src/ImageProcessor.UnitTests/Processors/RotateInsideUnitTests.cs

@ -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)
{
}
}
}

41
src/ImageProcessor/Processors/RotateInside.cs

@ -74,32 +74,21 @@ namespace ImageProcessor.Processors
return image;
}
/// <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="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 cref="T:ImageProcessor.Processors.Rotate"/>
/// </remarks>
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;

Loading…
Cancel
Save