Graphics and Bitmaps: Copying a Picture



Copying a picture is the process of getting each pixel of a picture from one document, the source, and reproducing it on another document in the exact same location (x and y coordinates), the target:



Copying a Picture

This operator is easy. From the source, get the (x, y) coordinate of a pixel and assign it to the corresponding (x, y) coordinate on the target document. 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,

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

let main argv =
    Application.Run exercise

This would produce:

Copying a Bitmap


Home Copyright © 2015 FunctionX, Inc. Home