Results 1 to 10 of 10

Thread: Refreshing Picturebox Automatically ?

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    5

    Refreshing Picturebox Automatically ?

    Hey guys.
    I'm new to VB programming and I'm trying to develop a program where you browse and select a picture, then it opens in a Picturebox. Afterwards you can choose to open that same picture in paint.
    My question is, after opening it in paint, making changes to it and saving the picture, how can I make it refresh automatically on the Picturebox with the new changes ?

    The code I'm using to open my image in paint is the following :

    If CBopen.SelectedItem = "Paint" Then
    Process.Start("mspaint.exe", ImgPath)
    End If

    I managed to do it by adding a "Refresh" button with this code :

    ImgPath = OpenFileDialog.FileName
    PictureBox1.ImageLocation = ImgPath

    but I want it to do it automatically after I save the picture on paint.

    Can anyone help me ?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Refreshing Picturebox Automatically ?

    You would have to use a FileSystemWatcher to watch that file for changes. When a change is detected, then you can reload the PictureBox. You should start by reading about the FileSystemWatcher class and check out some examples of its use on the web. Post back here if you strike issues when you try to use it.

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    5

    Re: Refreshing Picturebox Automatically ?

    Quote Originally Posted by jmcilhinney View Post
    You would have to use a FileSystemWatcher to watch that file for changes. When a change is detected, then you can reload the PictureBox. You should start by reading about the FileSystemWatcher class and check out some examples of its use on the web. Post back here if you strike issues when you try to use it.
    Okay thanks I've looked into it and learned how to use it to monitor a specific folder and write to a txt file or to a listbox when something in that folder is changed,deleted,etc, but I still didn't figure out how to use it to monitor the specific image selected in the dialog, which gets saved to my Path variable..

    What code would I use to do that ?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Refreshing Picturebox Automatically ?

    You know how to monitor a folder. When something happens in that folder, you get notified. If what you get notified about is what you're interested in, i.e. type of operation and path, you do whatever's appropriate in response.

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    5

    Re: Refreshing Picturebox Automatically ?

    Quote Originally Posted by jmcilhinney View Post
    You know how to monitor a folder. When something happens in that folder, you get notified. If what you get notified about is what you're interested in, i.e. type of operation and path, you do whatever's appropriate in response.
    Hmm wouldn't this be right to do what I want?

    Private Sub FileSystemWatcher1_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed

    FileSystemWatcher1.Path = OpenFileDialog.FileName
    PictureBox1.ImageLocation = OpenFileDialog.Filename


    The filesystemwatcher1.Path is the path I want it to be monitoring, which is the one chosen on the OpenFileDialog.Filename since I want it to monitor that specific File and supposedly after it has been changed it should update the picture on the picturebox1.imagelocation with that new changed file..

    Whats missing here?
    Last edited by Davez91; May 29th, 2011 at 11:23 AM.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Refreshing Picturebox Automatically ?

    There's no use setting the Path of the FileSystemWatcher in its own Changed event handler because how is the Changed event ever going to be raised if the Path hasn't been already set? Also, as the documentation says, the Path property is the path of a folder, not a file.

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    5

    Re: Refreshing Picturebox Automatically ?

    Quote Originally Posted by jmcilhinney View Post
    There's no use setting the Path of the FileSystemWatcher in its own Changed event handler because how is the Changed event ever going to be raised if the Path hasn't been already set? Also, as the documentation says, the Path property is the path of a folder, not a file.
    Oh yes you are right. So I should set the Path of the fsw on the Form_Load right? But then how do I set its Path to the specific folder where my image is ?
    Cause I am already using a Openfiledialog to select my image directly.
    Do I have to use Folderbrowserdialog along with the openfiledialog to get this working ?
    Is it even possible to monitor a specific file with FSW or do I really need to monitor an entire folder ? Cause from what I've found it targets Path if you are using Openfiledialog and SelectedPath if you are using a Folderbrowserdialog, so I'm a bit confused on how to get it to just monitor a specific file which it's location gets stored in my Imgpath variable.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Refreshing Picturebox Automatically ?

    Do you know the path of the folder you want to monitor when the form loads? No you don't, so how can you set the Path of the FSW? When do you know the path you want to monitor? When the user selects the file, so that's when you set the Path of the FSW. When the Changed event is raised it means that a change has occurred in that folder. You need to then determine whether the change was to the file you're interested in. Which file is that? The one whose path you assigned to the ImageLocation of the PictureBox.

  9. #9

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    5

    Re: Refreshing Picturebox Automatically ?

    Quote Originally Posted by jmcilhinney View Post
    Do you know the path of the folder you want to monitor when the form loads? No you don't, so how can you set the Path of the FSW? When do you know the path you want to monitor? When the user selects the file, so that's when you set the Path of the FSW. When the Changed event is raised it means that a change has occurred in that folder. You need to then determine whether the change was to the file you're interested in. Which file is that? The one whose path you assigned to the ImageLocation of the PictureBox.
    Okay I'm following that logic, so I would set the FSW Path after the user has selected the file and clicked OK on the OpenFileDialog, which means in my browse button.

    This is part only part of it, cause I also included a filter,etc :

    Private Sub cmdBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBrowse.Click

    If OpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
    lbl1.Text = "Image Loaded"
    FileSystemWatcher1.Path = ???

    Else : lbl1.Text = ""

    End If

    The question is, how do I give FSW the Path of the image that the user selected on the OpenFileDialog ? I searched around but I didn't find the syntax to do so..how do I get something to tell me the Pathway of a file I select ?
    Last edited by Davez91; May 31st, 2011 at 01:47 PM.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Refreshing Picturebox Automatically ?

    You're using an OpenFileDialog. Have you read the documentation for the OpenFileDialog class? That will tell you. Have you looked online for examples using the OpenFileDialog class? Most, although not all, of those will also show you. The information you want is very, very easy to find. I'm happy to help with the hard stuff but this is not hard.

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