mirror of https://github.com/SixLabors/ImageSharp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
4.6 KiB
119 lines
4.6 KiB
// --------------------------------------------------------------------------------------------------------------------
|
|
// <copyright file="Rotate.cs" company="James South">
|
|
// Copyright (c) James South.
|
|
// Licensed under the Apache License, Version 2.0.
|
|
// </copyright>
|
|
// <summary>
|
|
// Encapsulates methods to rotate an image.
|
|
// </summary>
|
|
// --------------------------------------------------------------------------------------------------------------------
|
|
|
|
namespace ImageProcessor.Processors
|
|
{
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Drawing.Drawing2D;
|
|
using ImageProcessor.Common.Exceptions;
|
|
|
|
/// <summary>
|
|
/// Encapsulates the methods to rotate the inside of an image
|
|
/// </summary>
|
|
public class RotateInside : IGraphicsProcessor
|
|
{
|
|
/// <summary>
|
|
/// Gets or sets the DynamicParameter.
|
|
/// </summary>
|
|
public dynamic DynamicParameter { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets any additional settings required by the processor.
|
|
/// </summary>
|
|
public Dictionary<string, string> Settings { get; set; }
|
|
|
|
/// <summary>
|
|
/// Processes the image.
|
|
/// </summary>
|
|
/// <param name="factory">The current instance of the <see cref="T:ImageProcessor.ImageFactory" /> class containing
|
|
/// the image to process.</param>
|
|
/// <returns>
|
|
/// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory" /> class.
|
|
/// </returns>
|
|
/// <remarks>
|
|
/// Based on <see href="http://math.stackexchange.com/questions/1070853/"/>
|
|
/// </remarks>
|
|
public Image ProcessImage(ImageFactory factory)
|
|
{
|
|
Bitmap newImage = null;
|
|
Image image = factory.Image;
|
|
|
|
try
|
|
{
|
|
float angle = 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);
|
|
|
|
image.Dispose();
|
|
image = newImage;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (newImage != null)
|
|
{
|
|
newImage.Dispose();
|
|
}
|
|
|
|
throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
|
|
}
|
|
|
|
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)
|
|
{
|
|
// Create a new empty bitmap to hold rotated image
|
|
Bitmap newImage = new Bitmap(image.Width, image.Height);
|
|
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;
|
|
|
|
// Put the rotation point in the "center" of the image
|
|
graphics.TranslateTransform(rotateAtX, rotateAtY);
|
|
|
|
// Rotate the image
|
|
graphics.RotateTransform(angle);
|
|
|
|
// Move the image back
|
|
graphics.TranslateTransform(-rotateAtX * 2, -rotateAtY * 2);
|
|
|
|
// Draw passed in image onto graphics object
|
|
graphics.DrawImage(image, new PointF(rotateAtX, rotateAtY));
|
|
}
|
|
|
|
return newImage;
|
|
}
|
|
}
|
|
}
|