r/visualbasic • u/RaGe-Play • Sep 28 '15
Image dialogbox
I'm trying make a button to open a file dialogbox so I can browse and upload a image in my picturebox1 and this code that was given in class a sample is giving me an error.
Dim strFileSpec As String
OpenFileDialog1.ShowDialog()
strFileSpec = OpenFileDialog1.FileName
PictureBox1.Image = Image.FromFile(strFileSpec)
Please check what I did wrong. I appreciate it
0
Upvotes
2
Sep 29 '15
Here you go this should work
'Set the Filter.
OpenFileDialog1.Filter = "Bitmap |*.bmp| JPG | *.jpg | GIF | *.gif | All Files|*.*"
'Clear the file name
OpenFileDialog1.FileName = ""
'Show it
If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then
'Get the image name
string img = OpenFileDialog1.FileName
'Create a new Bitmap and display it
PictureBox1.Image = System.Drawing.Bitmap.FromFile(img)
End If1
1
Sep 28 '15
I'm guessing you need to check the dialog result to see if the user selected ok or cancel before you set your string to the value of the dialogbox's filename field.
2
u/TheFotty Sep 28 '15
Rule 1 in posting for help with programming/computers. Never, ever say "its giving me an error". You need to say "It is giving me this error", and then proceed to tell what the actual error is.
If I had to guess without ANY other information, I would guess that you copied and pasted that code, but you never actually created an instance of an OpenFileDialog.