Add Multiple Pictures Dynamically and Show the Selected Picture in VB.NET

Submitted by: 
Visitors have accessed this post 5143 times.

This tutorial will teach you how to create a program that will add multiple pictures dynamically and will show the selected picture in the picturebox using vb.net.

1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application.

2. Next, add only one Button named Button1 and one PictureBox named PictureBox1 in your Form.

3. Now, we will do the coding.

We will first declare the variables that we will going to use specially the OpenFileDialog that will filter all the image files.

  1. Dim ofd As New OpenFileDialog With {.Filter = "Images|*.jpg;*.bmp;*.png;*.gif;*.wmf"}
  2. Dim pic As PictureBox
  3. Dim wid As Int32

Next, we will convert the pictures to be send it into the picturebox after clicking it. Meaning, this will be displayed into the picturebox after clicking one image in the form.

  1. Sub convertPic(ByVal sender As System.Object, ByVal e As System.EventArgs)
  2. 'CONVERT SENDER INTO PICTUREBOX
  3. pic = CType(sender, PictureBox)
  4. PictureBox1.Image = pic.Image
  5. End Sub

Now, we will code for our browse buttons that will add multiple images to the form.

  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2. ofd.Multiselect = True
  3. If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
  4. For Each t In ofd.FileNames
  5. pic = New PictureBox
  6. pic.Image = Image.FromFile(t)
  7. pic.SizeMode = PictureBoxSizeMode.Zoom
  8. pic.SetBounds(wid, 10, 100, 100)
  9. wid += 105
  10. AddHandler pic.Click, AddressOf convertPic
  11. Me.Controls.Add(pic)
  12. Next
  13. End If
  14. End Sub

Here's the full source code:

  1. Public Class Form1
  2. Dim ofd As New OpenFileDialog With {.Filter = "Images|*.jpg;*.bmp;*.png;*.gif;*.wmf"}
  3. Dim pic As PictureBox
  4. Dim wid As Int32
  5. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  6. ofd.Multiselect = True
  7. If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
  8. For Each t In ofd.FileNames
  9. pic = New PictureBox
  10. pic.Image = Image.FromFile(t)
  11. pic.SizeMode = PictureBoxSizeMode.Zoom
  12. pic.SetBounds(wid, 10, 100, 100)
  13. wid += 105
  14. AddHandler pic.Click, AddressOf convertPic
  15. Me.Controls.Add(pic)
  16. Next
  17. End If
  18. End Sub
  19.  
  20. Sub convertPic(ByVal sender As System.Object, ByVal e As System.EventArgs)
  21. 'CONVERT SENDER INTO PICTUREBOX
  22. pic = CType(sender, PictureBox)
  23. PictureBox1.Image = pic.Image
  24. End Sub
  25. End Class

Output:

output

For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.

Best Regards,

Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware


Comments

this is very nice code example, i'm actually implementing this example with a similar program i have but i'm dealing with 60 or more pictures... it shows like 30 images only and gives me an error saying "out of memory"...can you help with this problem please?.. btw, i

You can use threading, maybe....

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.