5.2 Loading an Image using Open File Dialog Control
We have learned how to load an image in a picture box at the design phase using the properties window. Next, we shall learn how to load an image using the OpenFileDialog controls.
First, 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 letting the user browse his or her local drives, then select and open a file. For the OpenFileDialog to display all types of image files, we must specify the types of image files under the Filter property. Before that, rename OpenFileDialog as 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 also must delete the default Filename.
Figure 5.5
Next, double-click on the View button and enter the code as in Example 5.2
Example 5.2 Loading an Image via a Dialog Box
Private Sub BtnLoadImg_Click(sender As Object, e As EventArgs) Handles Button1.Click If OFGSelectImage.ShowDialog = Windows.Forms.DialogResult.OK Then End If |
Press F5 to run the program and click the View button, a dialog box showing all the image files will appear, as shown in Figure 5.6
Figure 5.6
Notice that the default image file is JPEG as we have placed it in the first position in the Filter property. Selecting and opening an image file will load it in the picture box, as shown in Figure 5.7
Figure 5.7
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.
0 Comments
Thanks for your comment.
i will reply you soon.