Image References

jhp531

New member
Joined
Jun 30, 2009
Messages
2
Programming Experience
1-3
Hi, I've looked in a few places but haven't had much luck in finding what I'm after.

So I have a picturebox on my windows form and it has an image set to it, I also have a bunch of reference files that reference to a bunch of images.

VB.NET:
Me.PictureBox1.Image = My.Resources.imgs.image1

That piece of code works.

Now, I tried to databind the picture box to a database so the images would load on the change of a combo box but that got confusing really fast so what I did was created a database and it holds all the image references such as "My.Resources.imgs.image1" but its stored as type varchar(50).

Connecting to the database and retreiving the strings is no problem, that works all fine.

The problem is converting the string to the image reference, it doesn't work. I've tried type casting, I tried using imagelocation rather than image but I want to reference to the resources that is attached to the project.

Can anyone help me with this issue? - Thanks

Here is the full code, incase it helps

VB.NET:
Imports System.Data.SqlClient 'before class constructor    

Dim SQLStr As String    
Private ConnString As String    
'after class constructor'the following is within a sub        

ConnString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\statsdata.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"        

SQLStr = "SELECT imgreference FROM tblImages WHERE (number = " + Me.ComboBox1.SelectedIndex + ")"        

Dim SQLConn As New SqlConnection() 'The SQL Connection        
Dim SQLCmd As New SqlCommand() 'The SQL Command       
Dim SQLdr As SqlDataReader        'The Local Data Store        
Dim text As String        
Dim img As Image = Nothing        

SQLConn.ConnectionString = ConnString 'Set the Connection String  
Try            
SQLConn.Open() 'Open the connection            
SQLCmd.Connection = SQLConn 'Sets the Connection to use with the SQL Command            
SQLCmd.CommandText = SQLStr 'Sets the SQL String            
SQLdr = SQLCmd.ExecuteReader() 'Executes SQL Commands 

SQLdr.Read()            
text = SQLdr.GetString(0)            

SQLdr.Close() 'Close the SQLDataReader              
SQLConn.Close() 'Close the connection            

'This is where I would change the reference of the image           
'E.g. Me.PictureBox1.Image = text - except I have to cast text from type String to type Image                    

Catch ex As Exception            
MsgBox(ex.Message, MsgBoxStyle.Information, "Somethings wrong!!") 
End Try
 
You can't store that whole string, i.e. "My.Resources.imgs.image1", because that's no use to the system. You'll need to store just the resource name and you won't be able to access it via My.Resources.

I just created a new WinForms project and added an image resource from a file name ScreenShot.jpg. I would normally access that Image in code using My.Resources.ScreenShot. If I open the Solution Explorer, click the Show All Files button, expand My Project and Resources.resx and then open Resources.Designer.vb, I can find that property defined like so:
VB.NET:
Friend ReadOnly Property ScreenShot() As System.Drawing.Bitmap
    Get
        Dim obj As Object = ResourceManager.GetObject("ScreenShot", resourceCulture)
        Return CType(obj,System.Drawing.Bitmap)
    End Get
End Property
As you can see, My.Resources uses a string to get the resource anyway, so you can do exactly the same thing yourself.
 
'ResourceManager' here refers to a property of My.Resources, which is available outside the Resources module. My.Resources.ResourceManager...
 
Back
Top