Bitmap Operations: 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

System::Void btnMirror_Click(System::Object^  sender, System::EventArgs^  e)
	Graphics ^ graph = pbxSource->CreateGraphics();
    Bitmap ^ bmpSource = reinterpret_cast<Bitmap ^>(pbxSource->Image);
    Bitmap ^ bmpDestination =
                gcnew Bitmap(pbxTarget->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);

    pbxTarget->Image = bmpDestination;

Mirroring a Picture

To support mirroring, the Bitmap class inherits a method named RotateFlip from its parent Image class. Its syntax is:

    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:

System::Void Mirror_Paint(System::Object^  sender, 
			  System::Windows::Forms::PaintEventArgs^  e)
    Bitmap ^ bmpPicture = gcnew Bitmap(L"person.jpg");
    e->Graphics->DrawImage(bmpPicture, 10, 10);

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


Home Copyright © 2009-2016, FunctionX, Inc.