C# Programming > Drawing

C# Image Rotate

c# image rotate

Image Rotate

Rotating an image in .NET is quite efficient with GDI+. Programmers do not have to worry about the complex algorithms.

The trickiest part is rotating an image without clipping the corners that end up outside the bounds of the image.

Point Math

Before rotating an image, we need a function to rotate individual points. The application is going to need to rotate the corner points of a .NET image to accurately update the bounds of the transformed image. That way we can avoid clipping part of the image.

Rotating 2D points around an origin needs some basic math. We are not going to focus much on it here, the link is for VB.NET code, but it is very similar in C#.

Transformation

One of the ways to rotate an image with GDI+ is to use the Matrix transformation class. In fact, in order to rotate an image without clipping, the Matrix class is essential. The .NET class allows transformations to be applied afterwards using the MatrixOrder parameter. In our application, MatrixOrder will let the image be translated after it's been rotated. Thus clipping is avoided without affected the rest of the transformation.

Once the Matrix transformation is setup, it is applied with the Transformation property of a Graphics object. Then the bitmap is drawn as always with DrawImage.

Thus at no point does the programmer have to deal with transformation algorithms, making such operations during .NET image development simple.

Source Code

The sample image rotate .NET application shows how to rotate an image using only GDI+ and some basic math to prevent image clipping.

Back to C# Article List