Browse Source

Adds a 'rotate layer' to provide a new 'resize' parameter

Former-commit-id: f5ad9f7c78aae6a11245490e39274078489af081
Former-commit-id: 381fce4fcaaf617203e3e4e294146bbf1b7a5b38
pull/17/head
Thomas Broust 11 years ago
parent
commit
8082d35fc6
  1. 25
      src/ImageProcessor.UnitTests/ImageFactoryUnitTests.cs
  2. 4
      src/ImageProcessor/ImageFactory.cs
  3. 1
      src/ImageProcessor/ImageProcessor.csproj
  4. 30
      src/ImageProcessor/Imaging/RotateInsideLayer.cs
  5. 13
      src/ImageProcessor/Processors/RotateInside.cs

25
src/ImageProcessor.UnitTests/ImageFactoryUnitTests.cs

@ -466,19 +466,38 @@ namespace ImageProcessor.UnitTests
}
}
/// <summary>
/// Tests that the image's inside is rotated
/// </summary>
[Test]
public void ImageIsRotatedInside()
{
int i = 0;
foreach (ImageFactory imageFactory in this.ListInputImages())
{
Image original = (Image)imageFactory.Image.Clone();
imageFactory.RotateInside(45);
imageFactory.RotateInside(new RotateInsideLayer { Angle = 45, KeepImageDimensions = true });
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");
}
}
/// <summary>
/// Tests that the image's inside is rotated and resized
/// </summary>
[Test]
public void ImageIsRotatedInsideAndResized()
{
int i = 0;
foreach (ImageFactory imageFactory in this.ListInputImages())
{
Image original = (Image)imageFactory.Image.Clone();
imageFactory.RotateInside(new RotateInsideLayer { Angle = 45, KeepImageDimensions = false });
imageFactory.Image.Width.Should().NotBe(original.Width, "because the rotated image dimensions should have changed");
imageFactory.Image.Height.Should().NotBe(original.Height, "because the rotated image dimensions should have changed");
imageFactory.Format(new ImageProcessor.Imaging.Formats.JpegFormat()).Save("./output/rotateinside-" + i++.ToString() + ".jpg");
imageFactory.Format(new ImageProcessor.Imaging.Formats.JpegFormat()).Save("./output/rotateinsideresized-" + i++.ToString() + ".jpg");
}
}

4
src/ImageProcessor/ImageFactory.cs

@ -902,11 +902,11 @@ namespace ImageProcessor
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public ImageFactory RotateInside(float degrees)
public ImageFactory RotateInside(RotateInsideLayer rotateLayer)
{
if (this.ShouldProcess)
{
RotateInside rotate = new RotateInside { DynamicParameter = degrees };
RotateInside rotate = new RotateInside { DynamicParameter = rotateLayer };
this.CurrentImageFormat.ApplyProcessor(rotate.ProcessImage, this);
}

1
src/ImageProcessor/ImageProcessor.csproj

@ -214,6 +214,7 @@
<Compile Include="Imaging\Filters\Photo\IMatrixFilter.cs" />
<Compile Include="Imaging\ResizeMode.cs" />
<Compile Include="Imaging\Resizer.cs" />
<Compile Include="Imaging\RotateInsideLayer.cs" />
<Compile Include="Imaging\Rotation.cs" />
<Compile Include="Imaging\RoundedCornerLayer.cs" />
<Compile Include="Imaging\TextLayer.cs" />

30
src/ImageProcessor/Imaging/RotateInsideLayer.cs

@ -0,0 +1,30 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ImageLayer.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Encapsulates the properties required to add an rotation layer to an image.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Imaging
{
/// <summary>
/// A rotation layer to apply an inside rotation to an image
/// </summary>
public class RotateInsideLayer
{
/// <summary>
/// Gets or sets the rotation angle.
/// </summary>
public float Angle { get; set; }
/// <summary>
/// Gets or sets a value indicating whether to keep the image dimensions.
/// If set to true, the image is zoomed inside the area.
/// If set to false, the area is resized to match the rotated image.
/// </summary>
public bool KeepImageDimensions { get; set; }
}
}

13
src/ImageProcessor/Processors/RotateInside.cs

@ -4,7 +4,7 @@
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Encapsulates methods to rotate an image.
// Encapsulates methods to rotate the inside of an image.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
@ -15,6 +15,7 @@ namespace ImageProcessor.Processors
using System.Drawing;
using System.Drawing.Drawing2D;
using ImageProcessor.Common.Exceptions;
using ImageProcessor.Imaging;
/// <summary>
/// Encapsulates the methods to rotate the inside of an image
@ -49,14 +50,14 @@ namespace ImageProcessor.Processors
try
{
float angle = this.DynamicParameter;
RotateInsideLayer rotateLayer = this.DynamicParameter;
// Center of the image
float rotateAtX = Math.Abs(image.Width / 2);
float rotateAtY = Math.Abs(image.Height / 2);
// Create a rotated image.
newImage = this.RotateImage(image, rotateAtX, rotateAtY, angle);
newImage = this.RotateImage(image, rotateAtX, rotateAtY, rotateLayer);
image.Dispose();
image = newImage;
@ -85,13 +86,13 @@ namespace ImageProcessor.Processors
/// <remarks>
/// Based on the Rotate effect
/// </remarks>
private Bitmap RotateImage(Image image, float rotateAtX, float rotateAtY, float angle)
private Bitmap RotateImage(Image image, float rotateAtX, float rotateAtY, RotateInsideLayer rotateLayer)
{
// Create a new empty bitmap to hold rotated image
Bitmap newImage = new Bitmap(image.Width, image.Height);
newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
float zoom = Imaging.Rotation.ZoomAfterRotation(image.Width, image.Height, angle);
float zoom = Imaging.Rotation.ZoomAfterRotation(image.Width, image.Height, rotateLayer.Angle);
// Make a graphics object from the empty bitmap
using (Graphics graphics = Graphics.FromImage(newImage))
@ -106,7 +107,7 @@ namespace ImageProcessor.Processors
graphics.TranslateTransform(rotateAtX, rotateAtY);
// Rotate the image
graphics.RotateTransform(angle);
graphics.RotateTransform(rotateLayer.Angle);
// Zooms the image to fit the area
graphics.ScaleTransform(zoom, zoom);

Loading…
Cancel
Save