Ministry of Higher Education
Ghor Institute of Higher Education
Computer Science Department
VB.NET
Handling Images
Presented by :hamidullah ahmadi
Email : hamidullahdadras@gmail.com
Contents
❖ Learn to load image in a picture box
❖ Learn to browse and load images using the common dialog
Loading an Image at Design Time
In this section, let us develop an image viewer . To create an image viewer,
we insert a picture box in the form. Next, change its border property to
FixedSingle and its background color to white. You might also want to
change the SizeMode property of the image to StretchImage so that the image
can fit in the picture box. In the properties window, scroll to the Image
property.
Con…
The next step is to select local resource and click on the Import button to
view the available image files in your local drives, as shown in Figure 5.3.
Finally, select the image you like and then click the open button, the image
will be displayed in the picture box, as shown in Figure 5.4
Loading an Image at Runtime
We can also load an image at runtime, using the code as follows:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles
MyBase.Load
PictureBox1.Image = Image.FromFile("C:\Users\Toshiba\Pictures\My
Pictures\USA\Chicago 2012.jpg")
End Sub
* You need to search for an image in your local drive and determine its path.
Running the program will display the same image in the picture box as in Figure 5.4
Loading an Image using Open File Dialog Control
To load an image in a picture box using the OpenFileDialog control, we must add the
OpenFileDialog control on the form. This control will be invisible during runtime, but it
facilitates the process of launching a dialog box and let the user browse his or her local drives and
then select and open a file. For the OpenFileDialogto display all types of image files, we must
specify the types of image files under the Filter property. Before that, rename OpenFileDialogas
OFGSelectImage. Next, right click on the OpenFileDialog control to access its properties
window. Beside the Filter property, specify the image files using the format:
JPEG Files| *.JPG|GIF Files|*.GIF|WIndows Bitmaps|*.BMP
as shown in Figure 5.5. These are the common image file formats. Besides that, you need
to delete the default Filename.
Con…
Con…
Next, double-click on the View button and enter the following code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
Button1.Click
If OFGSelectImage.ShowDialog = Windows.Forms.DialogResult.OK
Then
PictureBox1.Image = Image.FromFile(OFGSelectImage.FileName)
End If
End Sub
Summary
● In section 5.11, you have learned how to load an image at design
time using the properties window
● In section 5.1.2, you have learned how to load an image at runtime
● In section 5.2, you have learned how to load an image using the
OpenFileDialog control
Be Success!
Any Question?