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

Private Sub BtnMirror_Click(ByVal sender As System.Object,
                                ByVal e As System.EventArgs) Handles BtnMirror.Click
        Dim clr As Color
        Dim graph As Graphics
        Dim bmpSource As Bitmap
        Dim bmpDestination As Bitmap
        Dim x As Integer, y As Integer
        Dim intHeight As Integer, intWidth As Integer

        graph = pbxSource.CreateGraphics()
        bmpSource = pbxSource.Image
        bmpDestination =
           New Bitmap(pbxDestination.ClientSize.Width,

        intWidth = bmpSource.Width
        intHeight = bmpSource.Height

        For x = 0 To intWidth - 1
            For y = 0 To intHeight - 1
                clr = bmpSource.GetPixel(x, y)
                bmpDestination.SetPixel(intWidth - x - 1, y, clr)

        pbxDestination.Image = bmpDestination
    End Sub

Mirroring a Picture

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

Public Sub RotateFlip(rotateFlipType As 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 Sub BtnShowPictureClick(ByVal sender As System.Object, 
                                     ByVal e As System.EventArgs) 
                                     Handles BtnShowPicture.Click
        Dim bmpPicture As Bitmap = New Bitmap("woman.jpg")
        CreateGraphics().DrawImage(bmpPicture, 10, 10)
End Sub

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


Home Copyright © 2008-2012 FunctionX