Operations on Bitmaps
Scaling, Mirroring, Flipping, and Rotating a Picture
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:
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
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.
When it comes to bitmaps, mirroring is the process of changing the horizontal direction of a picture. For example imagine you have the picture of a person directed to the right:
With mirroring, you can make that face look to the left:
To support mirroring, the Bitmap class inherits a method named RotateFlip from its parent Image class. Its syntax is:
Public Sub RotateFlip(rotateFlipType As RotateFlipType)
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:
Private Sub btnShowPicture_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnShowPicture.Click Dim bmpPicture As Bitmap = New Bitmap("woman.jpg") bmpPicture.RotateFlip(RotateFlipType.RotateNoneFlipX) CreateGraphics().DrawImage(bmpPicture, 10, 10) End Sub
When this method is called, the bitmap horizontal direction would be changed.
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:
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:
Imports System.Drawing Imports System.Windows.Forms Module Exercise Public Class Starter Inherits Form Dim components As System.ComponentModel.Container Public Sub New() InitializeComponent() End Sub Public Sub InitializeComponent() End Sub Private Sub FormPainter(ByVal sender As Object, _ ByVal e As PaintEventArgs) _ Handles Me.Paint Dim bmpPicture As Bitmap = New Bitmap("Pushpin.gif") bmpPicture.RotateFlip(RotateFlipType.RotateNoneFlipY) e.Graphics.DrawImage(bmpPicture, 10, 10) End Sub End Class Function Main() As Integer Dim frmStart As Starter = New Starter Application.Run(frmStart) Return 0 End Function End Module
Rotating a picture consists of changing its angular orientation. The Image class supports this operation (with somewhat limited options) through its RotateFlip() method. As mentioned for mirroring and flipping, the value you pass as argument would determine how the operation is carried out.
The members of the RotateFlipType enumeration that are used to rotate a picture are:
As we have seen regularly in this and as you will see in other lessons, you can display a picture directly on a form. Still, to give you a more comfortable space to show a picture, the .NET Framework provides a Windows control named PicturePox.
Like most controls and as we described in our introduction to control design, to get a picture box, from the Toolbox, you can click the PictureBox button and click the form. To programmatically get the control, you can create a handle to the PictureBox class. Before using the control, make sure you add it to the list of Controls of the form that will host it. Here is an example:
Imports System.Drawing Imports System.Windows.Forms Module Exercise Public Class Starter Inherits Form Private pctBox As PictureBox Dim components As System.ComponentModel.Container Public Sub New() InitializeComponent() End Sub Public Sub InitializeComponent() pctBox = New PictureBox() Controls.Add(pctBox) End Sub End Class Function Main() As Integer Dim frmStart As Starter = New Starter Application.Run(frmStart) Return 0 End Function End Module
As a regular visual control, after a picture box has been added to a form, it assumes a default size (unless you dragged and drew when adding it). You can then move or resize it using the techniques of application design we reviewed in Lessons 3 and 4. Of course, you can also specify the location and the size of the control programmatically. Here is an example:
Public Sub InitializeComponent() pctBox = New PictureBox() pctBox.Location = New Point(10, 10) pctBox.Size = New Size(200, 150) Controls.Add(pctBox) End Sub
By default, when you have added a picture box to a form, it appears without borders. At run time, the form designer would show the borders so you can conveniently move or resize it:
At run time, unless you display an object in the picture box or draw a shape in it, the user would not know where the picture box starts and where it ends:
There is no way of knowing that the above form contains a picture box. If you want the picture box to show its borders to the user, you can use the BorderStyle property. We described this property in Lesson 5.
As stated in the introduction, the primary purpose of a picture box is to display a picture. To support this, the PictureBox class is equipped with a property named Image. This property is of type Image. At design time, to specify the picture that this control would hold, first click the picture box on the form to select it:
In both cases, a dialog box would come up to assist you with locating and selecting a picture.
To programmatically specify the picture to display, assign an Image object to the instance of the PictureBox class. Here is an example:
Public Sub InitializeComponent() pctBox = New PictureBox() pctBox.Location = New Point(10, 10) pctBox.Size = New Size(200, 150) pctBox.Image = Image.FromFile("person.gif") Controls.Add(pctBox) End Sub
If you decide to specify the picture programmatically, make sure you provide a valid picture or a valid path to the picture; otherwise you would receive an error if the application cannot find the image.
Besides the PictureBox.Image property, to assist you with specifying the image to display, the PictureBox class provides a property named ImageLocation. This property, which is of type String, expects either the path to the file or the URL of the image. At design time, difference with the Image property is that you are not asked to selected a picture but to give its location. Therefore, to use it, in the Properties window, type the complete path:
Remember that you can also provide a URL to the picture:
In both cases, if you provide a bad path or a broken link, that is, if the compiler cannot find the image, the picture box would display an X icon on its body.
At run time, you can also specify the path or the URL to the picture by assigning it to the PictureBox.ImageLocation property. Here is an example:
Public Sub InitializeComponent() pctBox = New PictureBox() pctBox.Location = New Point(10, 10) pctBox.Size = New Size(200, 150) pctBox.ImageLocation = "http://www.functionx.com/cars/civic.gif" Controls.Add(pctBox) End Sub
After assigning a string to the ImageLocation property, you can call the PictureBox.Load() method to actually show the image. This method is overloaded with two versions.
Instead of assigning a string to the PictureBox.ImageLocation property and calling the parameter-less PictureBox.Load() method, you can call the other version of the method. Its syntax is:
Public Sub Load(url As String)
This version takes as argument the URL of, or the path to, the picture. Here is an example:
Public Sub InitializeComponent() pctBox = New PictureBox() pctBox.Location = New Point(10, 10) pctBox.Size = New Size(200, 150) pctBox.Load("person.gif") Controls.Add(pctBox) End Sub
After you have specified the image that the picture box would display, by default, it is located from the top-left corner of the control. In some cases, for example if the picture's size is lower than the control's, this would be fine and you may not need to be concerned with that:
The picture box can show only as far as its size. If an image goes beyond the control, its parts would be hidden. In some cases, the image may appear too wide, too narrow, too tall, or too short for the picture box. And in some cases, if the image's size is higher than the picture box, the control would not show some important aspects. Therefore, in some cases, you want to resize either the picture to fit the control, or the control to fit the picture. In some cases, you can programmatically resize a control by changing its Size property. On the other hand, you can scale a picture as we learned with bitmap. The PictureBox class provides an alternative.
The SizeMode property of the PictureBox class allows you to specify how the image in the control would be displayed. This property is based on the PictureBoxSizeMode enumeration and its members are as follows:
|Previous||Copyright © 2008-2009, yevol.com||Next|