C# - Image rotation by angle

By , 9/15/2008
(1 ratings)
Rotates the image by angle.
/// <summary>
/// Rotates the image by angle.
/// </summary>
/// <param name="oldBitmap">The old bitmap.</param>
/// <param name="angle">The angle.</param>
/// <returns></returns>
private static Bitmap RotateImageByAngle(System.Drawing.Image oldBitmap, float angle)
    var newBitmap = new Bitmap(oldBitmap.Width, oldBitmap.Height);
    var graphics = Graphics.FromImage(newBitmap);
    graphics.TranslateTransform((float)oldBitmap.Width / 2, (float)oldBitmap.Height / 2);
    graphics.TranslateTransform(-(float)oldBitmap.Width / 2, -(float)oldBitmap.Height / 2);
    graphics.DrawImage(oldBitmap, new Point(0, 0));
    return newBitmap;

Tagged with image, angel, turn, rotate, bitmap.


By bruslee, 10/22/2010
Due the fact, that the default dpi in the new Bitmap is 96 dpi, you also have to set the correct resolution from the original image

[code=C#]newBitmap.SetResolution(oldBitmap.HorizontalResolution, oldBitmap.VerticalResolution); [/code]
By marrisa, 6/25/2013
This is a good code, but i found a simpler way and i'd like to share it.
Imports System.IO
Imports System.Drawing.Printing
Imports RasterEdge.Imaging
Imports RasterEdge.Imaging.Processing

Dim Image As New RasterEdgeImaging()

Public Sub RotateImage()
If True Then
Dim LoadImage As New Bitmap("C:\\1.bmp")
Dim rotate As Graphics = Graphics.FromImage(LoadImage)
rotate.TranslateTransform(CType(bmp.Width, Single) / 2, CType(bmp.Height, Single) / 2)
rotate.TranslateTransform(-CType(bmp.Width, Single) / 2, -CType(bmp.Height, Single) / 2)
rotate.InterpolationMode = InterpolationMode.HighQualityBicubic
rotate.DrawImage(img, New Point(0, 0))
Return LoadImage()
End If
End Sub

for any doubt, you can visit the http://www.rasteredge.com/how-to/vb-net-imaging/rotate-image/ rotate images method for solution.

Log in, to comment!