Results 1 to 5 of 5

Thread: [2005] Allow user to insert an image into a form

  1. #1

    Thread Starter
    New Member lpd0084's Avatar
    Join Date
    Apr 2006
    Posts
    7

    Question [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?

  2. #2
    Addicted Member dim_kevin_as_human's Avatar
    Join Date
    Oct 2005
    Location
    Wisconsin
    Posts
    183

    Re: [2005] Allow user to insert an image into a form

    this allows the user to load an image via a dialogbox.

    VB Code:
    1. If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    2.             PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
    3. 'else etc..etc...
    4.         End If
    Dreaming men are haunted men.

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2005] Allow user to insert an image into a form

    You may want to filter for valid image file types.
    VB Code:
    1. Private Sub cmdBrowsePic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBrowsePic.Click
    2.     With Me.dlgOpenFile
    3.         .Filter = "Image files only (.gif, .jpg, bmp)|*.gif; *.jpg; *.bmp"
    4.         .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)
    5.         If .ShowDialog = DialogResult.OK Then
    6.             Me.picLogo.Image = New Bitmap(.FileName())
    7.         End If
    8.     End With
    9. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  4. #4

    Thread Starter
    New Member lpd0084's Avatar
    Join Date
    Apr 2006
    Posts
    7

    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?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. myPictureBox.Load(myOpenFileDialog.FileName)
    2. 'OR
    3. 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:
    1. Using ofd As New OpenFileDialog
    2.     'Set properties of ofd here.
    3.  
    4.     If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
    5.         myPictureBox.Load(ofd.FileName)
    6.     End If
    7. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width