Home

Bitmap Operations: Flipping a Picture

   

Description

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 make the picture point up:

Flipping a Picture

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:

Flipping a Picture

System::Void btnFlip_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,
                             pbxTarget->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);
        }
    }

    pbxTarget->Image = bmpDestination;
}

Flipping a Picture

To support picture flipping, you can call the same RotateFlip() method of the Image class. This time, you would use a different value for the argument. The member of the RotateFlipType enumeration used to flip a picture is  RotateNoneFlipY. Here is an example of flipping a picture:

System::Void Flip_Paint(System::Object^  sender, 
			  System::Windows::Forms::PaintEventArgs^  e)
{
    Bitmap ^ bmpPicture = gcnew Bitmap(L"Pushpin.jpg");
    bmpPicture->RotateFlip(RotateFlipType::RotateNoneFlipY);
    e->Graphics->DrawImage(bmpPicture, 10, 10);
}
   
 
 
     
 

Home Copyright © 2009-2012 FunctionX, Inc.