Home

Graphics and Bitmaps: Mirroring a Picture

   

Description

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:

Mirror

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:

open System
open System.Drawing
open System.Windows.Forms

let mutable isSelecting = false

let exercise = new Form(MaximizeBox = true, Text = "Exercise",
                        ClientSize = new System.Drawing.Size(740, 430))

let pbxSource : PictureBox = new PictureBox(Size = new Size(346, 403),
                                            Location = new Point(12, 12),
                                            BorderStyle = BorderStyle.FixedSingle)
pbxSource.Image <- Image.FromFile(@"C:\Resources\Building1.jpg")
exercise.Controls.Add pbxSource

let bmpSource : Bitmap = new Bitmap(pbxSource.Image)

let pbxTarget : PictureBox = new PictureBox(Size = new Size(346, 403),
                                            Location = new Point(378, 12),
                                            BorderStyle = BorderStyle.FixedSingle)
exercise.Controls.Add pbxTarget
let bmpTarget : Bitmap = new Bitmap(pbxTarget.ClientSize.Width,
                                    pbxTarget.ClientSize.Height)

let width  = bmpSource.Width
let height = bmpSource.Height

for x in 0 .. width - 1 do
    for y in 0 .. height - 1 do
        let clr : Color = bmpSource.GetPixel(x, y)
        bmpTarget.SetPixel(width x - 1, y, clr)
pbxTarget.Image <- bmpTarget

[<STAThread>]
[<EntryPoint>]
let main argv =
    Application.Run exercise
    0

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:

member RotateFlip :
        rotateFlipType:RotateFlipType -> unit

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:

open System
open System.Drawing
open System.Windows.Forms

let mutable isSelecting = false

let exercise = new Form(MaximizeBox = true, Text = "Exercise",
                        ClientSize = new System.Drawing.Size(740, 430))

let pbxSource : PictureBox = new PictureBox(Size = new Size(346, 403),
                                            Location = new Point(12, 12),
                                            BorderStyle = BorderStyle.FixedSingle)
pbxSource.Image <- Image.FromFile(@"C:\Resources\Building1.jpg")
exercise.Controls.Add pbxSource

let pbxTarget : PictureBox = new PictureBox(Size = new Size(346, 403),
                                            Location = new Point(378, 12),
                                            BorderStyle = BorderStyle.FixedSingle)
exercise.Controls.Add pbxTarget

let bmpTarget : Bitmap = new Bitmap(pbxSource.Image)

bmpTarget.RotateFlip(RotateFlipType.RotateNoneFlipX)

pbxTarget.Image <- bmpTarget

[<STAThread>]
[<EntryPoint>]
let main argv =
    Application.Run exercise
    0

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

     
 

Home Copyright © 2015 FunctionX, Inc. Home