Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
Like this article? We recommend

Rotating and Flipping

Once you have an image loaded, you often need to view it from a different perspective. One example is the images on a photo CD. I often need to flip or rotate these images to see them correctly. It's not the fault of the processor; it's simply the fact that I turned the camera to take the picture, so the image is turned on the photo CD. Images often require flipping to produce a mirror image to use for artistic purposes. Here's an example that shows how to flip the image:

private void btnFlip_Click(object sender, System.EventArgs e)
{
  // Load the image.
  Image Temp = Image.FromFile("TestImage.gif");

  // Flip on the x axis.
  Temp.RotateFlip(RotateFlipType.RotateNoneFlipX);

  // Display the image onscreen.
  pbBitmap.Image = Temp;
  MessageBox.Show("Flip x axis");

  // Flip on the y axis.
  Temp.RotateFlip(RotateFlipType.RotateNoneFlipY);

  // Display the image onscreen.
  pbBitmap.Image = Temp;
  MessageBox.Show("Flip y axis");

  // Flip on both axes.
  Temp.RotateFlip(RotateFlipType.RotateNoneFlipXY);

  // Display the image onscreen.
  pbBitmap.Image = Temp;
  MessageBox.Show("Flip both axes");
}

Notice that you can flip on the x axis, y axis, or both axes without rotating the image. All you need is the RotateFlip() method and the correct constant to make the change. It's also possible to rotate the image. Images always rotate clockwise, but people don't always think in the clockwise direction. Consequently, when you want to rotate an image 90 degrees to the left, you actually need to rotate it 270 degrees clockwise. Combining rotating and flipping is also easy, as shown in this example:

private void btnRotate_Click(object sender, System.EventArgs e)
{
  // Load the image.
  Image Temp = Image.FromFile("TestImage.gif");

  // Rotate 90 degrees.
  Temp.RotateFlip(RotateFlipType.Rotate90FlipNone);

  // Display the image onscreen.
  pbBitmap.Image = Temp;
  MessageBox.Show("Rotated 90 degrees");

  // Rotate 90 degrees and flip on the x axis.
  Temp.RotateFlip(RotateFlipType.Rotate90FlipX);

  // Display the image onscreen.
  pbBitmap.Image = Temp;
  MessageBox.Show("Rotated 90 degrees and flipped on the x axis");
}

It's important to note that the .NET Framework doesn't provide an easy method for rotating an image in less than 90-degree increments. When you want to rotate an image 5 degrees, you need to use something a little more exotic than the RotateFlip() method.

  • + Share This
  • 🔖 Save To Your Account