I have an image box on my form. I want the user to be able to insert a pic from their hdd into the box. Can that be done?
Printable View
I have an image box on my form. I want the user to be able to insert a pic from their hdd into the box. Can that be done?
this allows the user to load an image via a dialogbox.
VB Code:
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName) 'else etc..etc... End If
You may want to filter for valid image file types.
VB Code:
Private Sub cmdBrowsePic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBrowsePic.Click With Me.dlgOpenFile .Filter = "Image files only (.gif, .jpg, bmp)|*.gif; *.jpg; *.bmp" .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) If .ShowDialog = DialogResult.OK Then Me.picLogo.Image = New Bitmap(.FileName()) End If End With End Sub
Thanx guys. I'll try these out later.
P.S. RobDog, I take it you don't care for firefox?
These two poors guys are living in the past, i.e. 2003. :) If you use Image.FromFile the file will remain locked until the Imge object is Disposed. The PictureBox in 2005 has new functionality that gets around that and also keeps a record of where the image came from, which may be useful if you want to save changes or whatever:When you load a file this way it does not remain locked and the path is stored in the ImageLocation property. Also, if you are creating an OpenFileDialog object at design time then you don't have to worry but if you are creating one on demand then make sure you Dispose it afterwards. You can do this with the Using statement in 2005:VB Code:
myPictureBox.Load(myOpenFileDialog.FileName) 'OR myPictureBox.ImageLocation = myOpenFileDialog.FileNameThe dialogue is Disposed at the End Using statement, which contains an implicit Try...Finally so the disposal occurs even if an exception is thrown.VB Code:
Using ofd As New OpenFileDialog 'Set properties of ofd here. If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then myPictureBox.Load(ofd.FileName) End If End Using