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


Private Sub Button1_Click(ByVal sender As System.Object,
                              ByVal e As System.EventArgs) Handles Button1.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(x, intHeight - y - 1, clr)

        pbxDestination.Image = bmpDestination
    End Sub


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:

Imports System.Drawing
Imports System.Windows.Forms

Module Exercise

    Public Class Starter
        Inherits Form

        Dim components As System.ComponentModel.Container

        Public Sub New()
        End Sub

        Public Sub InitializeComponent()

        End Sub

        Private Sub FormPainter(ByVal sender As Object, 
                                ByVal e As PaintEventArgs) 
                                Handles Me.Paint
            Dim bmpPicture As Bitmap = New Bitmap("Pushpin.gif")
            e.Graphics.DrawImage(bmpPicture, 10, 10)
        End Sub
    End Class

    Function Main() As Integer

        Dim frmStart As Starter = New Starter


        Return 0
    End Function

End Module




Home Copyright © 2008-2016, FunctionX, Inc.