Rotate an Image in VB NET and C#

Image Editing Tutorials: For C# and VB NET Students


In this section, we'll take a look at some of the ways you can edit and manipulate images with C# and Visual Basic .NET. You'll learn about image rotation, flipping, inverting, filtering, and how to access the individual pixels in an image. You'll also learn how to save the changes to your images.

Start a new project. Add a PictureBox control to your form. In the properties area on the right, locate the Image property. Click the button to the right of the Image property and load an image into your PictureBox control.

Image Rotation

Image rotation is quite easy, as there is a method called RotateFlip.

Add a button to your form. Set the Text property of the button to Rotate. Double click your new button to open the code stub for it. Now add the following line in C#:

Bitmap bmp = new Bitmap(pictureBox1.Image);

Or this in Visual Basic .Net:

Dim bmp As Bitmap = New Bitmap(PictureBox1.Image)

This line creates a new Bitmap object called bmp. There are quite a lot of overloads that the Bitmap constructor can take. One of these overloads just accepts an image object between the round brackets. We're using an existing image between the round brackets of our Bitmap constructor. The image is the one from the PictureBox called pictureBox1.

Once we have a Bitmap object to work with, we can use the RotateFlip method. Add this line to your code: (In VB, just delete the semicolon at the end.)

bmp.RotateFlip( RotateFlipType.Rotate90FlipNone);

In between the round brackets of RotateFlip you start with a RotateFlipType. As soon as you type a dot after RotateFlipType you'll see a list of options. These ones:

A context menu in Visual Studio showing a RotateFlipType option

We've gone for the one that rotates the image 90 degrees but doesn't flip it (you'll see what Flip does soon).

The only other line of code you need here is to assign your new bitmap image to the PictureBox Image property:


pictureBox1.Image = bmp;


PictureBox1.Image = bmp

When this line executes, your new Bitmap image will replace the image that was in the PictureBox previously.

Before trying it out, you can wrap all three lines of code in an if statement, just in case the picture box is empty:


if (pictureBox1.Image != null) {

else {

MessageBox.Show("No Image");



If PictureBox1.Image IsNot Nothing Then


MessageBox.Show("No Image")

End If

Your code should look like this in C#:

C# code showing how to rotate an image 90 degrees

And this in VB Net:

Visual Basic .NET code showing how to rotate an image 90 degrees

What the if statement does is to check if an image has been loaded into the PictureBox. This is done by testing for "Is Not Null" in C# and IsNot Nothing in VB Net.

Try it out. Run your program and load an image into the PictureBox, if you haven't already. Now click your button. Before rotating, our image looked like this:

An image before being rotated

After rotating 90 degrees, the image looks like this:

An image after it has been rotated 90 degrees

If you keep clicking your button, the image will rotate 90 degrees for each click, until it ends up in the same rotation.


Try out some of the other RotateFlipType options and see what they do.


In the next lesson below, we'll take a look at Image Flipping.

Image Flipping >>

Back to the C# NET Contents Page

Back to the VB NET Contents Page