You have an image which you want to rotate either clockwise or anticlockwise. You can do this easily using C#. In this article I will show you a simple example to rotate an image in C#. If you want to save that image after rotating, then you can see my article on how can you save an image using C#. I will be using a PictureBox to show the initial image then the rotated image.
Instructions:
- As we want to rotate the file both clockwise as well as anticlockwise we will call a method to rotate the image in which we will pass image and a string representing whether we want to rotate image to left side or right side.
Image image = Image.FromFile(@”D:test.jpg”);
image = rotateImage(pictureBox1.Image, "left");orImage image = Image.FromFile(@”D:test.jpg”);
image = rotateImage(pictureBox1.Image, "right"); - Here the code for that rotateImage functions which will rotate the image and will return the rotated image back.
private Image rotateImage(Image image, string p)
{
Bitmap bitmap = new Bitmap(image);
if (p == “left”)
bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
else if (p == “right”)
bitmap.RotateFlip(RotateFlipType.Rotate90FlipNone);
return (Image)(bitmap);
} - After this, you will see that your image rotates perfectly but if height and width of your image are not same then you may not be able see your full image. because when you rotate the image, its height become width of rotated image and width become height, but initially the size of PictureBox is same as that of image which will not change when the image is rotated, so we have to manually set the size of PictureBox equal to the size of image. Once your rotatedFunction returns rotated image, assign this image to PictureBox and change width and height of PictureBox to match the width and height of rotated image.
pictureBox1.Image = image;
pictureBox1.Size = pictureBox1.Image.Size;
Here is snapshot of the code I am using to rotate the image.
![]()
You can also download my full project on Image manipulations like Loading Image, Cropping Image, Rotating Image, Resizing Image, Zoom In and Zoom Out Image, And Saving Image using PictureBox control and C# language from here.
Incoming search terms:
- asp net c# image tilt (1)
- how to rotate images from one picturebox to another in c# (1)
Hello sir,
It was awesome. It was really helpful for my project. Thank you very much.
regards
palkalai