'Getting random image file from Resources in VB.Net (2022), which then displays in a picturebox
Im doing something for programming class, which involves making a 'Snap!' game.
Basically the code runs like this:
Private Sub btnOne_Click(sender As Object, e As EventArgs) Handles btnOne.Click
picOne.Visible = True 'displays picOne'
lblDisplay.Text = "Owl" 'changes text of lblDisplay to "Owl"
btnOne.Enabled = False 'btnOne is disabled, making it impossible to click it.
End Sub
Private Sub btnTwo_Click(sender As Object, e As EventArgs) Handles btnTwo.Click
picTwo.Visible = True 'displays picOne'
If lblDisplay.Text = "Bee" Then 'Checks if lblDisplay is "Bee"
lblDisplay.Text = "Snap!" 'displays 'Snap!' statement above is true
Else
lblDisplay.Text = "Bee" 'otherwise, just displays bee, as duplicate isnt found
End If
btnTwo.Enabled = False 'btnTwo is disabled, making it impossible to click it.
End Sub
However, this is extremely boring, as the same two cards will always be 'Snap!' I need a way so that a random image is selected from 'Resources,' and is then displayed in a picture box.
I'm not sure how I would go about getting a random image from resources, so if anyone could help me with this, I would be very grateful.
Thanks.
Solution 1:[1]
You know what images you have in resources so you can hard-code their names in a String array, generate a random index into that array, get the name and then get the resource with that name from the resource manager.
Dim imageResourceNames = {"Image1", "Image2", "Image3"}
Dim rng As New Random
Dim index = rng.Next(imageResourceNames.Length)
Dim imageResourceName = imageResourceNames(index)
Dim img = DirectCast(My.Resources.ResourceManager.GetObject(imageResourceName), Image)
If you prefer not to hard-code the resource names, you can get them using similar logic to one of the answers provided in the link in the comments:
Private Function GetResourceImageNames() As String()
Dim properties As PropertyInfo() = GetType(My.Resources.Resources).GetProperties(BindingFlags.NonPublic Or
BindingFlags.Instance Or
BindingFlags.Static)
Dim imageType As Type = GetType(Image)
Return properties.Where(Function(pi) imageType.IsAssignableFrom(pi.PropertyType)).
Select(Function(pi) pi.Name).
ToArray()
End Function
and:
Dim imageResourceNames = GetResourceImageNames()
Dim rng As New Random
Dim index = rng.Next(imageResourceNames.Length)
Dim imageResourceName = imageResourceNames(index)
Dim img = DirectCast(My.Resources.ResourceManager.GetObject(imageResourceName), Image)
Getting the names first and then getting just the one desired Image is more efficient than getting all Images and discarding all but one. Of course, if you plan to use all or most of the Images, then it might make sense to get them all into an array first and then use that array over and over to get random Images.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
