Operations on Bitmaps: Flipping a Picture
Flipping a picture consists of changing its vertical direction. For example you may have a picture that seems to be oriented down. By flipping, you can…
Introduction#
Flipping an image involves reversing its vertical orientation. For instance, a picture that appears to be pointing downwards can be flipped so that it points upwards instead:
To flip a picture, get each of its pixels from one side of its vertical position and transfer it to the opposite vertical position. The horizontal position of each pixel stays the same. This can be done as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Flip
{
public partial class Exercise : Form
{
public Exercise()
{
InitializeComponent();
}
private void btnFlip_Click(object sender, EventArgs e)
{
Graphics graph = pbxSource.CreateGraphics();
Bitmap bmpSource = (Bitmap)pbxSource.Image;
Bitmap bmpDestination =
new Bitmap(pbxDestination.ClientSize.Width,
pbxDestination.ClientSize.Height);
int width = bmpSource.Width;
int height = bmpSource.Height;
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Color clr = bmpSource.GetPixel(x, y);
bmpDestination.SetPixel(x, height - y - 1, clr);
}
}
pbxDestination.Image = bmpDestination;
}
}
}To perform a vertical flip, iterate through each pixel and move it to the mirrored vertical position while maintaining its original horizontal coordinate. Use the following logic:
private void btnManipulate_Click(object sender, EventArgs e)
{
Bitmap bmpPicture = new Bitmap("Pushpin.jpg");
bmpPicture.RotateFlip(RotateFlipType.RotateNoneFlipY);
CreateGraphics().DrawImage(bmpPicture, 10, 10);
}You can also achieve a flip by calling the RotateFlip() method from the Image class. This requires passing a specific enumeration member, RotateNoneFlipY, as the argument. The following example shows this technique:

