|
-
Jul 7th, 2015, 06:47 AM
#1
Thread Starter
New Member
VB Express 2010 Hundreds of Pictures chosen randomly
I want to be able to randomly choose a picture of a student from hundreds of possible ones and display it in a picture box.
Using "Select Case intRndStudent" works fine when tested for 14 students
Code:
Select Case intRndStudent
Case 1
picStudent.Image = My.Resources.1
Case 2
picStudent.Image = My.Resources.2
but the code will be incredibly long and adding another picture is a chore
I want to use a variable but I can't get it to work.
Code:
intRndStudent = Int(Rnd() * intNumPics) + 1 ' choose a picture at random
txtOutput.Text = intRndStudent ' used to check that random number is being chosen correctly
picStudent.Image = My.Resources.intRndStudent ' this code is bad - Error 1 'intRndStudent' is not a member of 'Resources'.
Would someone please tell me why this does not work and how to make it work?
-
Jul 7th, 2015, 07:23 AM
#2
Re: VB Express 2010 Hundreds of Pictures chosen randomly
In my case, I have only three resource images. This code will add each of them to a list of images, which can then be loaded into a picturebox via a random number generator. See if you can adapt to your needs:
Code:
Public Class Form1
Dim picList As New List(Of Image)
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim dEntry As DictionaryEntry
Dim rSet As Resources.ResourceSet = My.Resources.ResourceManager.GetResourceSet _
(Globalization.CultureInfo.CurrentCulture, True, True)
For Each dEntry In rSet.OfType(Of Object)()
If TypeOf (dEntry.Value) Is System.Drawing.Image Then
picList.Add(dEntry.Value)
picList(picList.Count - 1).Tag = dEntry.Key
End If
Next
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim r As Random = New Random
Dim num As Integer
Dim lowerBound As Integer
Dim upperBound As Integer
lowerBound = 0
upperBound = 2
num = r.Next(lowerBound, upperBound + 1) 'results in a number between 0 and 2, inclusive
PictureBox1.Image = picList(num)
End Sub
End Class
-
Jul 7th, 2015, 01:25 PM
#3
Re: VB Express 2010 Hundreds of Pictures chosen randomly
If you're going to have mroe than just a few that you're going to choose from, then you might be better off putting the files into a folder, not as a resource... then you can use io.directory.getallfiles to get a list of the files... then create a random (note - use the random class, not the older rnd function) number between 0 and the array count (less 1 of course) to get the file name... once you have that, it's a simple matter to load it and badda bing badda boom.
-tg
-
Jul 7th, 2015, 02:43 PM
#4
Re: VB Express 2010 Hundreds of Pictures chosen randomly
I completely agree with TG on using a folder to hold all of the files. If nothing else, having hundreds of pictures in your resources will make your application look bloated. Here is an example of doing exactly what TG said:
Code:
Private images As New List(Of Bitmap)
Private r As New Random
Private Sub MyForm_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Dim files() As String = Io.Directory.GetAllFiles("MyPath/MyImageFolder")
For Each img As String In files
images.Add(New Bitmap(img))
Next
End Sub
Private Function GetRandomImage() As Bitmap
Return images.Item(r.Next(0, images.Count))
End Function
-
Jul 7th, 2015, 06:40 PM
#5
Thread Starter
New Member
Re: VB Express 2010 Hundreds of Pictures chosen randomly
Thankyou for your replies. I will have another go this evening and get back if I need further pointers.
-
Jul 8th, 2015, 05:51 AM
#6
Thread Starter
New Member
Re: VB Express 2010 Hundreds of Pictures chosen randomly
Code:
Dim files() As String = Io.Directory.GetAllFiles("MyPath/MyImageFolder")
VB tells me
'GetAllFiles' is not a member of 'System.IO.Directory'
Can you tell me why?
-
Jul 8th, 2015, 06:56 AM
#7
Re: VB Express 2010 Hundreds of Pictures chosen randomly
because it's GetFiles I was shooting from the hip from a system that didn't have VS.
FYI - any time you encounter something new from the framework... if you google "MSDN {term here}" 90% of the time you'll get the documentation for it right up front. In this case "MSDN GetAllFile" brings up "Did you mean 'GetFiles'?" and a bunch of links to the documentation for Directory.GetFiles. The first of which is the one I linked to above.
-tg
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|