|
-
Apr 30th, 2006, 03:01 PM
#1
Thread Starter
New Member
[2005] Allow user to insert an image into a form
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?
-
Apr 30th, 2006, 03:33 PM
#2
Addicted Member
Re: [2005] Allow user to insert an image into a form
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
Dreaming men are haunted men.
-
Apr 30th, 2006, 03:41 PM
#3
Re: [2005] Allow user to insert an image into a form
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
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 30th, 2006, 04:39 PM
#4
Thread Starter
New Member
Re: [2005] Allow user to insert an image into a form
Thanx guys. I'll try these out later.
P.S. RobDog, I take it you don't care for firefox?
-
Apr 30th, 2006, 07:23 PM
#5
Re: [2005] Allow user to insert an image into a form
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:
VB Code:
myPictureBox.Load(myOpenFileDialog.FileName)
'OR
myPictureBox.ImageLocation = myOpenFileDialog.FileName
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:
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
The dialogue is Disposed at the End Using statement, which contains an implicit Try...Finally so the disposal occurs even if an exception is thrown.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|