# Code Snippets #22 - Rotate Image

Published 8/6/2013 by createdbyx in Code Snippets | Programming
Tags: ,

Source (adapted for GenericImage) -> http://stackoverflow.com/a/11176961/341706

```/// <summary>
/// Rotates the image.
/// </summary>
/// <param name="image">
/// The source image to rotate.
/// </param>
/// <param name="angle">
/// The angle in degrees to rotate the image.
/// </param>
/// <param name="centerX">The x position of the rotation axis.</param>
/// <param name="centerY">The y position of the rotation axis.</param>
/// <returns>
/// Returns a new rotated image.
/// </returns>
public static GenericImage<T> Rotate<T>(this GenericImage<T> image, int centerX, int centerY, float angle)
{
// source (adapted for GenericImage) -> http://stackoverflow.com/a/11176961/341706
var radians = (Math.PI / 180) * angle;
var newImage = new GenericImage<T>(image.Width, image.Height);

for (var x = 0; x < image.Width; x++)
for (var y = 0; y < image.Height; y++)
{
var m = x - centerX;
var n = y - centerY;
var j = ((int)(m * cos + n * sin)) + centerX;
var k = ((int)(n * cos - m * sin)) + centerY;
if (j >= 0 && j < image.Width && k >= 0 && k < image.Height)
{
newImage[x, y] = image[j, k];
}
}

return newImage;
}```

• Comment
• Preview

# Created by: X

## Just another personal website in this crazy online world

 Dean Lunz (aka Created by: X) Computer programming nerd, and tech geek. About Me -- Resume