Problem with PictureBox >?  
Author Message
Dr.Virusi





PostPosted: Visual Basic Express Edition, Problem with PictureBox >? Top


 I want this photo Usa.jpg to show in picturebox .
  
          if TextBox1.Text = "Usa" then
          PictureBox.Show This --> Usa.jpg How

 ::In Visual Basic 2005 Express Edition::


Visual Studio Express Editions43  
 
 
Andrej Tozon





PostPosted: Visual Basic Express Edition, Problem with PictureBox >? Top

Hi,

the following assumes you have your photos stored in the c:\MyPhotos folder and all files are Jpegs (file extension is .jpg):

Dim imageFileTemplate As String = "c:\MyPhotos\{0}.jpg"
Dim path As String = String.Format(imageFileTemplate, TextBox1.Text)
If (My.Computer.FileSystem.FileExists(path)) Then
PictureBox1.Image = Image.FromFile(imageFileTemplate)
Else
...
End If

String.Format() function will replace the {0} part in imageFileTemplate string with whatever the contents of the TextBox1 is. The If statement checks if specified file exists and sets the PictureBox1's image if it does.

Hope this helps,

Andrej



 
 
Dr.Virusi





PostPosted: Visual Basic Express Edition, Problem with PictureBox >? Top

Thanks a lot !
Dr.Virusi

 
 
Dr.Virusi





PostPosted: Visual Basic Express Edition, Problem with PictureBox >? Top


If i tested another computers it working


 
 
Andrej Tozon





PostPosted: Visual Basic Express Edition, Problem with PictureBox >? Top

The other computer should have the photos in the same folder as your development computer. If the nature of your program permits, try to let the user pick the photo folder (with, for example, using the FolderBrowserDialog).

Andrej



 
 
Dr.Virusi





PostPosted: Visual Basic Express Edition, Problem with PictureBox >? Top

All my photos i have in Folder .
And i will create setup for this folder, and my setup this folder sent in "Program Files"
Working Another pc

 
 
Dr.Virusi





PostPosted: Visual Basic Express Edition, Problem with PictureBox >? Top


I have test another computer but not working not show photos Why



 
 
Andrej Tozon





PostPosted: Visual Basic Express Edition, Problem with PictureBox >? Top

Do you have the photos stored in the same folder as on your development computer Are you getting any error when running the application

Andrej



 
 
BortNE24





PostPosted: Visual Basic Express Edition, Problem with PictureBox >? Top

Using the code sample above, you need to copy the photos into a folder at c:\MyPhotos

After that it will probably work.



 
 
Tall Dude





PostPosted: Visual Basic Express Edition, Problem with PictureBox >? Top

Maybe something like this would work better:

Private Sub Setpicture()

Dim picture As Image = Nothing

Dim ofd As New OpenFileDialog

ofd.Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.TIF)|*.BMP;*.JPG;*.GIF;*.TIF"

ofd.Multiselect = False

ofd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyPictures

If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then

Try

picture = Image.FromFile(ofd.FileName)

Catch ex As Exception

MsgBox("Error opening file > " & ofd.FileName)

Exit Sub

End Try

Else

Exit Sub

End If

PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage

PictureBox1.Image = picture

End Sub



 
 
Dr.Virusi





PostPosted: Visual Basic Express Edition, Problem with PictureBox >? Top

Do you have the photos stored in the same folder as on your development computer Are you getting any error when running the application

Andrej



Look! Just i have 1folder and all my photos i have in this folder. But not working
When i debug my app, no i have not errors!.




 
 
Andrej Tozon





PostPosted: Visual Basic Express Edition, Problem with PictureBox >? Top

Look! Just i have 1folder and all my photos i have in this folder.

Yes, but which folder is it It's important for your application to know in which folder to look for the files to function correctly. If you don't know which folder is that or you want the user to pick a folder, implement something like Tall Dude has posted.

Andrej



 
 
Dr.Virusi





PostPosted: Visual Basic Express Edition, Problem with PictureBox >? Top

Look code here and see folder name!

If TextBox1.Text = "UK" Then

Dim imageFileTemplate As String = "C:\Documents and Settings\Kastriot\Desktop\Test 04\Test 04\MyPicture\UK.jpg"

Dim path As String = String.Format(imageFileTemplate, TextBox1.Text)

If (My.Computer.FileSystem.FileExists(path)) Then

PictureBox1.Image = Image.FromFile(imageFileTemplate)

Else

MessageBox.Show("Sorry, this templates is removed!")

End If

End If
------------------------------------------------------------



 
 
Andrej Tozon





PostPosted: Visual Basic Express Edition, Problem with PictureBox >? Top

Ok... I see your code currently works only if UK is entered in the TextBox1. Modify the code so it will try to load whatever is entered in the textbox:

Dim imageFileTemplate As String = "C:\Documents and Settings\Kastriot\Desktop\Test 04\Test 04\MyPicture\{0}.jpg"
Dim path As String = String
.Format(imageFileTemplate, TextBox1.Text)

If (My.Computer.FileSystem.FileExists(path)) Then
PictureBox1.Image = Image.FromFile(imageFileTemplate)
Else
MessageBox.Show("Sorry, this templates is removed!"
)
End If

Important note: your current path looks like a temporary one (..\Test 04\Test 04\...). If this is indeed a name of the folder that will exist on your client computer, that's fine. Otherwise, you should let user pick a folder containing the photos.

Andrej



 
 
Dr.Virusi





PostPosted: Visual Basic Express Edition, Problem with PictureBox >? Top


Not Working

Please can you create sample application with this code PictureBox ......
And Upload link here.

Please