Home

Operations on Bitmaps

 

Scaling a Picture

 

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 you can use. Of course, in order to scale a picture, you must first have one.

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

Public Sub New(original As Image, width As Integer, height As Integer)

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:

Picture Normal Size

Public Class Form1

    Private bmpPicture As Bitmap
    
    Private Sub Form1_Load(ByVal sender As System.Object, _
                           ByVal e As System.EventArgs) _
                           Handles MyBase.Load
        bmpPicture = New Bitmap(10, 10)
    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, _
                            ByVal e As System.Windows.Forms.PaintEventArgs) _
                            Handles Me.Paint
        e.Graphics.DrawImage(bmpPicture, 120, 12)
    End Sub

    Private Sub btnShowPicture_Click(ByVal sender As System.Object, _
                                     ByVal e As System.EventArgs) _
                                     Handles btnShowPicture.Click
        Dim dlgOpen As OpenFileDialog
        Dim strFilename As String
        Dim Width As Integer

        dlgOpen = New OpenFileDialog()

        If dlgOpen.ShowDialog() = Windows.Forms.DialogResult.OK Then
            strFilename = dlgOpen.FileName
            bmpPicture = New Bitmap(strFilename)
            Width = bmpPicture.Width
            Int(Height = bmpPicture.Height)

            TextBox1.Text = Width.ToString()
            TextBox2.Text = Height.ToString()

            Invalidate()
        End If
    End Sub

End Class

Picture Resized

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

Public Sub New(original As Image, newSize As Size)

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 © 2008-2016, FunctionX, Inc.