I have image resources embedded into my project with names such as _1, _2 etc To load them into a picturebox is simple enough:
PictureBox1.Image = My.Resources._1
If I generate random numbers, then build a string, I can't pass this string to My.Resources.(string here). For Example:
' Random number generated as "value" is 1 for example a$="My.Resources._" & Str(value) PictureBox1.Image = a$ 'Can't cast string to System.Drawing.Bitmap
How can I pass a resource name to the PictureBox from my string? Or is there a better way to do this?
Any suggestions appreciated. |
| tangent685 Monday, October 19, 2009 12:52 AM |
If you have 70 images check this code, When you press thebutton1will select one of the 70 images randomly and put it in picturebox1 This much shorter than with the Select case
Option Strict On
Imports System.Resources
Imports System.Collections
Public Class Form1
Dim ResourceName As New List(Of String)
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim reader As New ResourceReader("Resources.resources")
Dim Enumerator As IDictionaryEnumerator = reader.GetEnumerator()
While Enumerator.MoveNext()
If Enumerator.Value.ToString = "System.Drawing.Bitmap" Then
ResourceName.Add(CStr(Enumerator.Key))
End If
End While
reader.Close()
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Rndl As New Random
Dim R As Integer = Rndl.Next(ResourceName.Count)
PictureBox1.Image = DirectCast(My.Resources.ResourceManager.GetObject(ResourceName.Item(R)), System.Drawing.Image)
End Sub
End Class
- Marked As Answer bytangent685 22 hours 56 minutes ago
-
|
| Crazypennie 23 hours 22 minutes ago |
This is a way to do this. Somebody may correct me here, but I believe that using the type Object like I did it in this code does create an array of reference and not an array of bitmap. This using much less resource
Option Strict On
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim BM() As Object = {My.Resources.untitled23, My.Resources.road}
'let suppose that you want the second image
PictureBox1.Image = DirectCast(BM(1), Bitmap)
End Sub
End Class
|
| Crazypennie Monday, October 19, 2009 2:16 AM |
You cannot pass string as bitmap, you can try to do it using select case or if statement
Dim rd as new Random
Dim GenerateRandomNumber as Integer
GenerateRandomNumber=rd.Next(1, 3)
Select Case GenerateRandomNumber
Case 1
Me.PictureBox1.Image = My.Resources._1
Case 2
Me.PictureBox1.Image = My.Resources._2
End Select
kaymaf
I hope this helps, if that is what you want, just mark it as answer so that we can move on
|
| kaymaf Monday, October 19, 2009 2:25 AM |
First get a random number. Then use a Select Case block to get the random image. Example if you have 5 images:
Dim randnum As New Random() value = randnum.Next(1, 5) Select Casevalue Case 1 PictureBox1.Image = My.Resources.MISC34 Case 2 PictureBox1.Image = My.Resources.FACE02 Case 3 'etc up to 5 End Select
You can't build a string out of My.Resources or an image.
Why are you using a $ after a? And why are you using Str()? This looks like QBasic code.
Solitaire |
| Solitaire Monday, October 19, 2009 2:35 AM |
This would work, and I considered this approach. I have over 70 images, however, and that would make for a lot of Case statements. That is why I was trying to figure out a way to pass (or cast)a string, or another (faster)method altogether.
Similarly with Crazypennie's approach. I would have to type out all the array elements because I could only load strings with a For/Next loop.
|
| tangent685 Monday, October 19, 2009 11:54 PM |
If you have 70 images check this code, When you press thebutton1will select one of the 70 images randomly and put it in picturebox1 This much shorter than with the Select case
Option Strict On
Imports System.Resources
Imports System.Collections
Public Class Form1
Dim ResourceName As New List(Of String)
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim reader As New ResourceReader("Resources.resources")
Dim Enumerator As IDictionaryEnumerator = reader.GetEnumerator()
While Enumerator.MoveNext()
If Enumerator.Value.ToString = "System.Drawing.Bitmap" Then
ResourceName.Add(CStr(Enumerator.Key))
End If
End While
reader.Close()
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Rndl As New Random
Dim R As Integer = Rndl.Next(ResourceName.Count)
PictureBox1.Image = DirectCast(My.Resources.ResourceManager.GetObject(ResourceName.Item(R)), System.Drawing.Image)
End Sub
End Class
- Marked As Answer bytangent685 22 hours 56 minutes ago
-
|
| Crazypennie 23 hours 22 minutes ago |
Thanks Crazypennie. I think this is the way to go. I was trying to figure out how to use ResourceReader to do this and you have provided a good example. |
| tangent685 22 hours 56 minutes ago |