Inserting Pictures in a PictureBox

Tyecom

Well-known member
Joined
Aug 8, 2007
Messages
78
Programming Experience
Beginner
Hi All,

I've been at this for awhile. I really appreciate any help I can get. I have a form with one PictureBox, one TextBox and a Button. I want to be able to enter a number in the TextBox, click the Button and have a Picture associated to that number displayed in the PictureBox. I have a folder that holds different images (C:\Images\0001.jpg, 0002.jpg, 0003.jpg, etc). I have the code to display "one" picture in the PictureBox: (MovieImage = Image.FromFile("C:\Images\0003.jpg"), but again, I want to be able to display images based on the number entered in the textbox. Basically, I want to be able to change (C:\Images\0001.jpg) to whatever number I enter in the textbox. Thank you in advance!
 
VB.NET:
Dim number As Integer

If Integer.TryParse(Me.TextBox1.Text, number) Then
    Dim path As String = String.Format("C:\Images\{0:0000}.jpg", number)

    'Use path here to create Image.
Else
    'Invalid input.
End If
You might like to try a NumericUpDown rather than a TextBox, so no validation or conversion is needed.
 
jmcilhinney,

Thank you very much for responding. I really appreciate it. However, I'm still having trouble getting this to work. Here is my code below:

VB.NET:
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim number As Integer
        If Integer.TryParse(Me.TextBox1.Text, number) Then
            Dim path As String = String.Format("C:\Images\{0:0000}.jpg", number)
            'Use path here to create Image.
            PictureBox1.Image = Image.FromFile(My.Computer.FileSystem.SpecialDirectories.MyPictures & "0001.jpg")
            'Invalid input.
            MsgBox("Movie Not Found")
        End If
    End Sub
End Class

I don't know if I understood your "Use path here to create image" note. Can you please explain?
 
Last edited by a moderator:
This line:
VB.NET:
Dim path As String = String.Format("C:\Images\{0:0000}.jpg", number)
creates a String containing the path of the image file and assigns it to the 'path' variable. As the comment says, you're then supposed to use that 'path' variable to create your Image object? How do you create an Image object? You call Image.FromFile and pass the path as an argument. You've already created the path so you don't then create it again. If you need to use a different folder then simply change the folder in that line of code I provided.
 
jmcilhinney,

Thank you very much! It worked! I can't tell you how much I appreciate this. I'm fairly new with programming, but really enjoy learning how to do different things. Again, Thank you very much!
 
Back
Top