dotnet-Snippets.com
Snippets: 73 | Registered User: 84 | Visitors online: 19
Main Menu

Home
Random Snippet
FAQs
Contact Us
Imprint
RSS Feeds

Rss All languages
Rss C#
Rss VB.NET
Rss C++
Rss J#
Rss ASP.NET
Jobs

dotnet Jobs
Google Ads

Sri Lanka .NET 
                Forum Member
Image rotation by angle

Author: Jan Welker
Programming Language: C# Rating:
not yet rated

Views: 11191

Description:

Rotates the image by angle.



C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/// <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.RotateTransform(angle);
    graphics.TranslateTransform(-(float)oldBitmap.Width / 2, -(float)oldBitmap.Height / 2);
    graphics.DrawImage(oldBitmap, new Point(0, 0));
    return newBitmap;
}


This Snippets could be interesting for you:

Poor Excellent
1 2 3 4 5 6 7 8 9 10
Sign in to vote for this snippet.

Comments:
(Please log in to write an comment.)

bruslee wrote on: 22.10.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
C#
1
newBitmap.SetResolution(oldBitmap.HorizontalResolution, oldBitmap.VerticalResolution);