Operations on Bitmaps: Mirroring a Picture



Mirroring a picture consists of changing the horizontal direction of a picture. This is done by transferring each pixel from the source of the picture to a target picture on the opposite side:


To mirror a picture, get each of its pixels from one side of its horizontal position and transfer it to the opposite side of the horizon position. The vertical position of each pixel stays the same. This can be done as follows:

Mirroring a Picture

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 Mirror
    public partial class Exercise : Form
        public Exercise()

        private void btnMirror_Click(object sender, EventArgs e)
            Graphics graph = pbxSource.CreateGraphics();
            Bitmap bmpSource = (Bitmap)pbxSource.Image;
            Bitmap bmpDestination =
                new Bitmap(pbxDestination.ClientSize.Width,

            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(width - x - 1, y, clr);

            pbxDestination.Image = bmpDestination;

Mirroring a Picture

Instead of writing you own code, to assist you with picture mirroring, the Bitmap class inherits a method named RotateFlip from its parent Image class. Its syntax is:

public void RotateFlip(RotateFlipType rotateFlipType);

This function takes one argument that specifies the mirroring option through the RotateFlipType enumeration. The members of  the RotateFlipType enumeration that can be used to mirror a picture are RotateNoneFlipX and Rotate180FlipY. Here is an example of mirroring a picture:

private void btnManipulate_Click(object sender, EventArgs e)
        Bitmap bmpPicture = new Bitmap("person.jpg");
        CreateGraphics().DrawImage(bmpPicture, 10, 10);

When this method is called, the bitmap horizontal direction would be changed.


Home Copyright © 2010-2016, FunctionX