C# Image Rotation
By FoxLearn 1/16/2025 1:56:40 AM 14
If you’re interested in learning more about the mathematical side of image rotation, you might want to explore image transformation matrices.
Creating the Rotation Method
The goal is to create a method that takes a Bitmap
object and a rotation angle (in degrees) and returns the rotated image.
private Bitmap RotateImage(Bitmap b, float angle) { // Step-by-step process will be explained below }
We pass in a Bitmap
object (the image we want to rotate) and the angle
(the degree of rotation, clockwise).
Create a New Bitmap and Graphics Object
First, we create a new empty Bitmap
that will hold the rotated image. This new bitmap will have the same dimensions as the input image. We then create a Graphics
object from the empty bitmap to perform the drawing operations.
private Bitmap RotateImage(Bitmap b, float angle) { // Create a new empty bitmap to hold the rotated image Bitmap returnBitmap = new Bitmap(b.Width, b.Height); // Create a graphics object from the empty bitmap Graphics g = Graphics.FromImage(returnBitmap); }
Move the Image to the Center
By default, an image rotates around its top-left corner. To rotate it around its center, we need to first move the image's rotation point to the center of the image.
g.TranslateTransform((float)b.Width / 2, (float)b.Height / 2);
Rotate the Image
Once the center of the image is set as the rotation point, we can apply the rotation using the RotateTransform
method. The angle
parameter determines how much the image should be rotated clockwise.
g.RotateTransform(angle);
Move the Image Back to its Original Position
After applying the rotation, we need to move the image back to its original position to prevent it from being displaced. This is done by applying a reverse translation using the TranslateTransform
method again.
g.TranslateTransform(-(float)b.Width / 2, -(float)b.Height / 2);
Draw the Image
Finally, we use the Graphics
object to draw the rotated image onto the new bitmap. The DrawImage
method is used to copy the original image into the graphics object.
g.DrawImage(b, new Point(0, 0));
Complete Code for Rotating an Image in C#
Putting all the steps together, here is the complete code for the RotateImage
method:
private Bitmap RotateImage(Bitmap b, float angle) { // Create a new empty bitmap to hold the rotated image Bitmap returnBitmap = new Bitmap(b.Width, b.Height); // Create a graphics object from the empty bitmap Graphics g = Graphics.FromImage(returnBitmap); // Move the rotation point to the center of the image g.TranslateTransform((float)b.Width / 2, (float)b.Height / 2); // Rotate the image by the specified angle g.RotateTransform(angle); // Move the image back to its original position g.TranslateTransform(-(float)b.Width / 2, -(float)b.Height / 2); // Draw the original image onto the graphics object g.DrawImage(b, new Point(0, 0)); // Return the rotated image return returnBitmap; }
In this tutorial, we learned how to rotate an image in C# by using the Graphics
object and transformation methods.
- How to fix 'Failure sending mail' in C#
- How to Parse a Comma-Separated String from App.config in C#
- How to convert a dictionary to a list in C#
- How to retrieve the Executable Path in C#
- How to validate an IP address in C#
- How to retrieve the Downloads Directory Path in C#
- C# Tutorial
- Dictionary with multiple values per key in C#