The Characteristics of a Picture Box |
|
Introduction |
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-2016, FunctionX, Inc. | Home |
|