Results 1 to 11 of 11

Thread: [2008] VB 2008 ImageBox File

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2008
    Posts
    11

    Thumbs up [2008] VB 2008 ImageBox File

    Hey There!

    Im Looking For A Code To Select An Image File And Put It Into An Image Box. It Should Be Fairly Simple. Thanks

    Ben

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2008] VB 2008 ImageBox File

    I'm not going to give you the code but rather guide you how to get it done... That way, you'll have a chance to learn.

    To allow the user to browse for a file, you'd use the OpenFileDiag control. To display the openfiledialog, you call its ShowDialog method. The openfiledialog returns a DialogResult value, which tells you what button on the openfiledialog was clicked by the user (OK or Cancel...). OpenFileDialog.Filter property allows you to specify a filter such that only certain file types will be displayed. The FileName property gives you the full path to the file selected by the user. Once you have the file path, you can use Image.FromFile(path) to create an Image object and then assign it to your PictureBox.Image property.
    Psuedo-code:
    - Create an OpenFileDialog object (can be dropped from the toolbox to your form in design view)
    - Upon an event (such as the click of a button), you show the openfiledialog
    - Get the returned result, and if it's the Dialogresult.OK then you read the file name from FileName property of the OFD.
    - Create an Image using Image.FromFile(fileName)
    - Assign the image to PictureBox.Image property

    That's all there is to it. And happy coding
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2008
    Posts
    11

    Talking Re: [2008] VB 2008 ImageBox File

    As I Am New To This Could You Please Give Me The Coding.

    I Can Do Most FO The Steps But It Doesnt Seem To Piece Togther

    Thanks

    Ben

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2008] VB 2008 ImageBox File

    You won't learn anything if everything is coded for you by someone else...
    But here it is. Put this code where you want to display the openfiledialog, i.e. in a button click event handler...
    Code:
          Using ofd As New OpenFileDialog()
                With ofd
                    .Filter = "Image Files|*.jpg;*.bmp;*.png;*.gif"
                    .Multiselect = False
                    .CheckFileExists = True
                    If .ShowDialog = Windows.Forms.DialogResult.OK Then
                        Dim img As Image = Image.FromFile(.FileName)
                        PictureBox1.Image = img
                    End If
                End With
            End Using
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2008
    Posts
    11

    Re: [2008] VB 2008 ImageBox File

    Thanks Much! I Am Slowloy Learning!

  6. #6

    Thread Starter
    New Member
    Join Date
    Jun 2008
    Posts
    11

    Re: [2008] VB 2008 ImageBox File

    As This Is A User Interpretd Program Is There Anyway I Could Make The Image Stay The Same Everytime The Program Is Started, Unless It Is Changed. Thanks! It Wouyld Be G00d If This Was Coding Aswell!

    Thnaks Again!

    Ben

  7. #7
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2008] VB 2008 ImageBox File

    Have a look at My.Settings... Basically you declare a setting of type String with user scope, and when your program loads, you pull that setting from My.Settings. If it is string.empty, you let the user pick an image, then load the image to your picturebox and you save the path to that image into My.Settings, else, you check to see the file exists and load the image using the path from My.Settings.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  8. #8

    Thread Starter
    New Member
    Join Date
    Jun 2008
    Posts
    11

    Re: [2008] VB 2008 ImageBox File

    Thanks! Youve Been A Good Help!

  9. #9

    Thread Starter
    New Member
    Join Date
    Jun 2008
    Posts
    11

    Re: [2008] VB 2008 ImageBox File

    The Code Unfoturaltly Doesn't Work....I Am Struggling As I Am Only 13 years Old! It Doesnt Seem To Pull The String

    Could I HAve Some Help Pelase!

    Thanks

  10. #10
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2008] VB 2008 ImageBox File

    Follow these steps:

    A. Creating a setting:
    1. Click Project in the menu bar, and choose [your project name] Properties...
    2. Click on the Settings tab
    3. Type in a new setting as followings:
    Name: LastImageFilePath
    Type: String
    Scope: User
    Value: leave it blank
    4. Save and close the application property window/tab.

    B: Read the setting
    In your form.load event handler:
    Code:
       Dim imagePath As String = My.Settings.LastImageFilePath
       If Not String.IsNullOrEmpty(imagePath) Then
          If System.IO.File.Exists(imagePath) Then
              PictureBox1.Image = Image.FromFile(imagePath)
          End If
       End If
    C: Save the setting:
    Suppose the user want to pick an image file, you show the openfiledialog as shown in my previous post. You just need to add code to save the image path (in rerd)
    Code:
                   If .ShowDialog = Windows.Forms.DialogResult.OK Then
                        My.Settings.LastImageFilePath = .FileName
                        My.Settings.Save()
                        Dim img As Image = Image.FromFile(.FileName)
                        PictureBox1.Image = img
                    End If
    And that's all there is to it.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  11. #11

    Thread Starter
    New Member
    Join Date
    Jun 2008
    Posts
    11

    Re: [2008] VB 2008 ImageBox File

    Thanks It Works Perfectly!

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