Home

Graphics and Bitmaps: Scaling a Picture

   

Description

Scaling a picture consists of changing its size, either to widen, to enlarge, to narrow, to heighten, or to shrink it. Although there are various complex algorithms you can use to perform this operation, the Bitmap class provides a shortcut. Of course, in order to scale a picture, you must first have one.

To support picture scaling, the Bitmap class provides the following constructor:

new :
        original : Image *
        width : int *
        height : int -> Bitmap

The original argument is the Image, such as a Bitmap object, that you want to scale. The width and the height arguments represent the new size you want to apply to the picture. After this constructor has been used, you get a bitmap with the new size. Here is an example of using it:

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(bmpSource, bmpSource.Width - 100, bmpSource.Height - 50)

pbxTarget.Image <- bmpTarget
pbxTarget.Size <- new Size(bmpTarget.Width, bmpTarget.Height)

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

Picture Resized

You can also specify both the width and the height as the size. To do this, you can use the following constructor:

new : 
        original:Image * 
        newSize:Size -> Bitmap

As you can see, the scaling operation could produce a kind of distorted picture. An alternative would be to keep the ration of both dimensions while changing the value of one.

     
     
 

Home Copyright © 2015, FunctionX Home