// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
//
//
// Encapsulates methods to rotate an image.
//
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Processors
{
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using ImageProcessor.Common.Exceptions;
///
/// Encapsulates the methods to rotate the inside of an image
///
public class RotateInside : IGraphicsProcessor
{
///
/// Gets or sets the DynamicParameter.
///
public dynamic DynamicParameter { get; set; }
///
/// Gets or sets any additional settings required by the processor.
///
public Dictionary Settings { get; set; }
///
/// Processes the image.
///
/// The current instance of the class containing
/// the image to process.
///
/// The processed image from the current instance of the class.
///
///
/// Based on
///
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;
}
///
/// Rotates the inside of an image to the given angle at the given position.
///
/// The image to rotate
/// The horizontal pixel coordinate at which to rotate the image.
/// The vertical pixel coordinate at which to rotate the image.
/// The angle in degrees at which to rotate the image.
/// The image rotated to the given angle at the given position.
///
/// Based on
///
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;
}
}
}