C#

Rotate image in C#

Let’s see how we can rotate an image in C#.

1
2
3
4
5
6
7
8
9
10
11
public Image RotateImage(Image img, float angle)
{
	Bitmap bim = new Bitmap(img.Width, img.Height);
	Graphics graph = Graphics.FromImage(bim);
	graph.TranslateTransform((float)img.Width / 2, (float)img.Height / 2);
	graph.RotateTransform(angle);
	graph.TranslateTransform(-(float)img.Width / 2, -(float)img.Height / 2);
	graph.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height));
 
	return bim;
}

How to use it:

float angle = 180;
RotateImage(Image.FromFile("filename"), angle).Save("rotatedimg");