Home

Graphics & Bitmaps: 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:

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(x, height - y - 1, clr)
pbxTarget.Image <- bmpTarget

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

Flip

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:

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.RotateNoneFlipY)

pbxTarget.Image <- bmpTarget

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

Home Copyright © 2015 FunctionX, Inc. Home