Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / C#

C# Image/PictureBox Rotations

Rate me:
Please Sign up or sign in to vote.
4.62/5 (25 votes)
16 Feb 2010CPOL1 min read 261.9K   20.7K   36   19
How to rotate any image from the center of that image

Introduction

I was working on an Etch A Sketch like program and made a knob image and wanted to simply rotate it as the user hit the arrow keys. I found many sources online, but none that got it done just how I wanted (see links below in References section). I then decided since I figured it out and got it all working why not make a simple demo with source that others could use.

Background

  • Loading an image from a dialogue prompt
  • Creating bitmaps, image/draw updates, and Graphic manipulation

Using the Code

There is a class called Utilities in the Utilities.cs file that contains the functions for rotating an image from the center or given an offset to rotate from.

Here is the main rotation function RotateImage:

C#
/// <summary>
/// Creates a new Image containing the same image only rotated
/// </summary>
/// <param name=""image"">The <see cref=""System.Drawing.Image"/"> to rotate
/// <param name=""offset"">The position to rotate from.
/// <param name=""angle"">The amount to rotate the image, clockwise, in degrees
/// <returns>A new <see cref=""System.Drawing.Bitmap"/"> of the same size rotated.</see>
/// <exception cref=""System.ArgumentNullException"">Thrown if <see cref=""image"/"> 
/// is null.</see>
public static Bitmap RotateImage(Image image, PointF offset, float angle)
{
    if (image == null)
        throw new ArgumentNullException("image");
        
    //create a new empty bitmap to hold rotated image
    Bitmap rotatedBmp = new Bitmap(image.Width, image.Height);
    rotatedBmp.SetResolution(image.HorizontalResolution, image.VerticalResolution);
    
    //make a graphics object from the empty bitmap
    Graphics g = Graphics.FromImage(rotatedBmp);
    
    //Put the rotation point in the center of the image
    g.TranslateTransform(offset.X, offset.Y);
    
    //rotate the image
    g.RotateTransform(angle);
    
    //move the image back
    g.TranslateTransform(-offset.X, -offset.Y);
    
    //draw passed in image onto graphics object
    g.DrawImage(image, new PointF(0, 0));
    
    return rotatedBmp;
}

And here is how it is used:

C#
//Load an image in from a file
Image image = new Bitmap("Picture.png");
//Set our picture box to that image
pictureBox.Image = (Bitmap)image.Clone();

//Store our old image so we can delete it
Image oldImage = pictureBox.Image;
//Pass in our original image and return a new image rotated 35 degrees right
pictureBox.Image = Utilities.RotateImage(image, 35);
if (oldImage != null)
{
    oldImage.Dispose();
} 

Points of Interest

Graphics are very powerful and allow you to do image manipulation really easily. For instance, this code could easily be modified to scale instead. I may added to this with more utilities like that.

References

History

  • 16th February, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionRotate image has three parameters by your example only passes 2 Pin
dcarl66112-Mar-21 11:25
dcarl66112-Mar-21 11:25 
QuestionCan we rotate picture box on mouse click ? Pin
Member 1137362726-Mar-17 23:11
Member 1137362726-Mar-17 23:11 
GeneralMy vote of 5 Pin
bayotle4-Jan-16 16:19
bayotle4-Jan-16 16:19 
QuestionHow its used missing PointF Pin
DBLWizard23-Sep-15 7:56
DBLWizard23-Sep-15 7:56 
QuestionError with "Utilities" PinPopular
Member 1126703226-Nov-14 11:48
Member 1126703226-Nov-14 11:48 
QuestionBlurry picture | Found solution :3 Pin
Jonathan SE11-Aug-14 2:27
Jonathan SE11-Aug-14 2:27 
GeneralThank U Pin
Member 1082376816-May-14 5:45
Member 1082376816-May-14 5:45 
QuestionThankful Pin
Member 1082376816-May-14 5:42
Member 1082376816-May-14 5:42 
Generalthanks Pin
Le Ngoc12-May-13 1:03
Le Ngoc12-May-13 1:03 
Questionthanks Pin
Uzzal Kanti Barua19-Mar-13 0:18
Uzzal Kanti Barua19-Mar-13 0:18 
GeneralMy vote of 4 Pin
Altaf N Patel6-May-12 21:04
Altaf N Patel6-May-12 21:04 
QuestionThanks - but for 90/180 degrees rotations... Pin
copa01721-Feb-12 11:49
copa01721-Feb-12 11:49 
AnswerRe: Thanks - but for 90/180 degrees rotations... Pin
MusicMonkey555519-Mar-13 7:21
MusicMonkey555519-Mar-13 7:21 
QuestionHere's a slightly more flexible version for rotating an image (with and without clipping), angle -180 to +180 degrees PinPopular
victorbos28-Oct-11 8:11
victorbos28-Oct-11 8:11 
AnswerRe: Here's a slightly more flexible version for rotating an image (with and without clipping), angle -180 to +180 degrees Pin
Member 190497013-Feb-12 3:12
Member 190497013-Feb-12 3:12 
GeneralThanx Pin
raananv29-May-11 21:12
raananv29-May-11 21:12 
GeneralMy vote of 5 Pin
atix-group15-Feb-11 3:59
atix-group15-Feb-11 3:59 
Generalthanks! Pin
peejay0224-Aug-10 0:01
peejay0224-Aug-10 0:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.