locked
Update/refresh/reload Image/picture (.jpg) in PictureBox every 5 minutes

    Question

  • Hi Guys!
    I have a wuestion about, how to Update/refresh/reload Image/picture (.jpg) in PictureBox every 5 minutes?
    I mean that every 5 minutes a picture is reloaded in a PictureBox control!
    What shall I do?
    Please help me!
    Monday, January 07, 2008 8:32 PM

Answers

  • Add a timer object to your form with its timeout property set to 300000(5 minutes) and double click on it.
    In the newly created tick handler add your code for loading the image in the picture box. The code will be called once every 5 minutes.

    Regards
    Monday, January 07, 2008 8:52 PM
  • Hi,

     

    I would get the Timer Tick event to call either

     

    Me.Refresh()

     

    'or

     

    Me.Invalidate()

     

    every 5 minutes.

    This will call the Paint events and the PictureBox will be re-painted.

     

    It is better to refresh the image in the Paint event as the image will also be refreshed if you minimise and maximise the FORM as well as if the end-user has another window temporarily in front of your application window.

    When your window is brought to the front it is also refreshed.

     

    Change the picture file name and path as appropriate.>>

    "C:\Documents and Settings\John\My Documents\My Pictures\2.jpg"

     

    Change the user name to the name of your folder.

     

     

     

     

    Regards,

     

    John

     

     

    Code Block

    Public Class Form1

     

     

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

     

    Timer1.Interval = 30000

    Timer1.Start()

    End Sub

     

     

     

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick

     

    Me.Refresh()

    End Sub

     

     

     

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

     

    PictureBox1.Image = Image.FromFile("C:\Documents and Settings\John\My Documents\My Pictures\2.jpg")

     

    End Sub

    End Class

     

     

     

     

    Tuesday, January 08, 2008 1:39 AM
  •  John Oliver (UK)MSP, VSIP wrote:

    Taking the random picture idea one stage further, how could you load all the pictures of say type *.jpg and *.bmp into a LIST (Of IMAGE) regardless of the filenames from the users My_Pictures folder ( and include the sub-folders in My_Pictures if possible )?

     

    I realise that adding all the PATHs to a LIST(Of String) may be possible and you could then use the ToArray method to put the LIST into an array.

     

    Then the RANDOM could be between zero and the Ubound of the array.

     

    Hi John,

     

    Here are the code samples.

     

    Approach 1: Load all images from My Picture folder to LIST (Of IMAGE)

    Code Block

    Imports System.IO

     

    Public Class Form1

        Dim r As New Random

        Dim ListImage As New List(Of Image)

     

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            Dim PicFolder As String = My.Computer.FileSystem.SpecialDirectories.MyPictures

            Dim dirPath As DirectoryInfo = New DirectoryInfo(PicFolder)

     

            For Each file As FileInfo In dirPath.GetFiles("*.jpg", SearchOption.AllDirectories)

                ListImage.Add(Image.FromFile(file.FullName))

            Next

            For Each file As FileInfo In dirPath.GetFiles("*.bmp", SearchOption.AllDirectories)

                ListImage.Add(Image.FromFile(file.FullName))

            Next

     

            MessageBox.Show("Image count " & ListImage.Count)

     

            Timer1.Interval = 2000

            Timer1.Start()

        End Sub

     

        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

            PictureBox1.Image = ListImage(r.Next(0, ListImage.Count))

        End Sub

    End Class

     

     

    Approach 2: Load all image path from My Picture folder to LIST (Of String).

    Code Block

    Imports System.IO

     

    Public Class Form2

        Dim r As New Random

        Dim ListPath As New List(Of String)

     

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

     

            Dim PicFolder As String = My.Computer.FileSystem.SpecialDirectories.MyPictures

            Dim dirPath As DirectoryInfo = New DirectoryInfo(PicFolder)

     

            For Each file As FileInfo In dirPath.GetFiles("*.jpg", SearchOption.AllDirectories)

                ListPath.Add(file.FullName)

            Next

            For Each file As FileInfo In dirPath.GetFiles("*.bmp", SearchOption.AllDirectories)

                Me.Text = file.FullName

                ListPath.Add(file.FullName)

            Next

     

            MessageBox.Show("Image count " & ListPath.Count)

     

            Timer1.Interval = 2000

            Timer1.Start()

        End Sub

     

        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

            PictureBox1.Image = Image.FromFile(ListPath(r.Next(0, ListPath.Count)))

        End Sub

    End Class

     

     

    Reference: DirectoryInfo.GetFiles Method (String, SearchOption)

    http://msdn2.microsoft.com/en-us/library/ms143327(VS.80).aspx

    Returns a file list from the current directory matching the given searchPattern and using a value to determine whether to search subdirectories.

     

    Regards,

    Martin

    Friday, January 11, 2008 7:31 AM

All replies

  • Add a timer object to your form with its timeout property set to 300000(5 minutes) and double click on it.
    In the newly created tick handler add your code for loading the image in the picture box. The code will be called once every 5 minutes.

    Regards
    Monday, January 07, 2008 8:52 PM
  • In the newly created tick handler add your code for loading the image in the picture box. The code will be called once every 5 minutes.

    What do you mean by loadnig an image in the picture box?
    I add PictureBox1.ImageLocation = "UrlOfTheFile" and do as you say!But nothing happens!
    Monday, January 07, 2008 9:39 PM
  • Hi,

     

    I would get the Timer Tick event to call either

     

    Me.Refresh()

     

    'or

     

    Me.Invalidate()

     

    every 5 minutes.

    This will call the Paint events and the PictureBox will be re-painted.

     

    It is better to refresh the image in the Paint event as the image will also be refreshed if you minimise and maximise the FORM as well as if the end-user has another window temporarily in front of your application window.

    When your window is brought to the front it is also refreshed.

     

    Change the picture file name and path as appropriate.>>

    "C:\Documents and Settings\John\My Documents\My Pictures\2.jpg"

     

    Change the user name to the name of your folder.

     

     

     

     

    Regards,

     

    John

     

     

    Code Block

    Public Class Form1

     

     

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

     

    Timer1.Interval = 30000

    Timer1.Start()

    End Sub

     

     

     

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick

     

    Me.Refresh()

    End Sub

     

     

     

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

     

    PictureBox1.Image = Image.FromFile("C:\Documents and Settings\John\My Documents\My Pictures\2.jpg")

     

    End Sub

    End Class

     

     

     

     

    Tuesday, January 08, 2008 1:39 AM
  •  Gromav wrote:

    I mean that every 5 minutes a picture is reloaded in a PictureBox control!
    ...

    I add PictureBox1.ImageLocation = "UrlOfTheFile" and do as you say!But nothing happens!

     

    Hi Gromav,

     

    Marcel and John are right. Thank you for your help!

     

    Here is additional code sample.

    To show the effect reloading an image, it randomly selects an image among 1.jpg and 2.jpg.

    Code Block

    Public Class Form1

        Dim r As New Random

     

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            Timer1.Interval = 2000 ' 2 seconds=2000 milliseconds. 5 minutes =300000 milliseconds

            Timer1.Start()

        End Sub

     

        Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick

            PictureBox1.Image = Image.FromFile("E:\VBProject\" & r.Next(1, 3) & ".jpg")

            ' or

            PictureBox1.ImageLocation = "E:\VBProject\" & r.Next(1, 3) & ".jpg"

        End Sub

    End Class

     

    Thursday, January 10, 2008 8:22 AM
  • Hi Martin,

     

    Taking the random picture idea one stage further, how could you load all the pictures of say type *.jpg and *.bmp into a LIST (Of IMAGE) regardless of the filenames from the users My_Pictures folder ( and include the sub-folders in My_Pictures if possible )?

     

    I realise that adding all the PATHs to a LIST(Of String) may be possible and you could then use the ToArray method to put the LIST into an array.

     

    Then the RANDOM could be between zero and the Ubound of the array.

     

    How easy would that be please?

    Would you be able to post the code to do this please?

     

     

     

     

    Regards,

     

    John

     

    Thursday, January 10, 2008 7:16 PM
  •  John Oliver (UK)MSP, VSIP wrote:

    Taking the random picture idea one stage further, how could you load all the pictures of say type *.jpg and *.bmp into a LIST (Of IMAGE) regardless of the filenames from the users My_Pictures folder ( and include the sub-folders in My_Pictures if possible )?

     

    I realise that adding all the PATHs to a LIST(Of String) may be possible and you could then use the ToArray method to put the LIST into an array.

     

    Then the RANDOM could be between zero and the Ubound of the array.

     

    Hi John,

     

    Here are the code samples.

     

    Approach 1: Load all images from My Picture folder to LIST (Of IMAGE)

    Code Block

    Imports System.IO

     

    Public Class Form1

        Dim r As New Random

        Dim ListImage As New List(Of Image)

     

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            Dim PicFolder As String = My.Computer.FileSystem.SpecialDirectories.MyPictures

            Dim dirPath As DirectoryInfo = New DirectoryInfo(PicFolder)

     

            For Each file As FileInfo In dirPath.GetFiles("*.jpg", SearchOption.AllDirectories)

                ListImage.Add(Image.FromFile(file.FullName))

            Next

            For Each file As FileInfo In dirPath.GetFiles("*.bmp", SearchOption.AllDirectories)

                ListImage.Add(Image.FromFile(file.FullName))

            Next

     

            MessageBox.Show("Image count " & ListImage.Count)

     

            Timer1.Interval = 2000

            Timer1.Start()

        End Sub

     

        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

            PictureBox1.Image = ListImage(r.Next(0, ListImage.Count))

        End Sub

    End Class

     

     

    Approach 2: Load all image path from My Picture folder to LIST (Of String).

    Code Block

    Imports System.IO

     

    Public Class Form2

        Dim r As New Random

        Dim ListPath As New List(Of String)

     

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

     

            Dim PicFolder As String = My.Computer.FileSystem.SpecialDirectories.MyPictures

            Dim dirPath As DirectoryInfo = New DirectoryInfo(PicFolder)

     

            For Each file As FileInfo In dirPath.GetFiles("*.jpg", SearchOption.AllDirectories)

                ListPath.Add(file.FullName)

            Next

            For Each file As FileInfo In dirPath.GetFiles("*.bmp", SearchOption.AllDirectories)

                Me.Text = file.FullName

                ListPath.Add(file.FullName)

            Next

     

            MessageBox.Show("Image count " & ListPath.Count)

     

            Timer1.Interval = 2000

            Timer1.Start()

        End Sub

     

        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

            PictureBox1.Image = Image.FromFile(ListPath(r.Next(0, ListPath.Count)))

        End Sub

    End Class

     

     

    Reference: DirectoryInfo.GetFiles Method (String, SearchOption)

    http://msdn2.microsoft.com/en-us/library/ms143327(VS.80).aspx

    Returns a file list from the current directory matching the given searchPattern and using a value to determine whether to search subdirectories.

     

    Regards,

    Martin

    Friday, January 11, 2008 7:31 AM
  • Hi again Martin,

     

    Thanks very much.

     

    I just noticed the slight difference between the two listings above however you named them both as Approach 1.

     

     

     

     

    Regards,

     

    John

     

    Saturday, January 12, 2008 12:28 PM
  •  John Oliver (UK)MSP, VSIP wrote:

    however you named them both as Approach 1.

     

    Oh! I'm careless. I have changed the second Approach 1 to Approach 2.

     

    Thank you John for your great help and contribution to MSDN forums!

    Monday, January 14, 2008 2:22 AM
  • For Each file As FileInfo In dirPath.GetFiles("*.bmp", SearchOption.AllDirectories)

                Me.Text = file.FullName

                ListPath.Add(file.FullName)

            Next



    Me.Text = file.FullName


    May I please ask the purpose of this line of code and why is it not in the .jpg portion of the For Each/Next code, thanks to Martin.

    Friday, January 18, 2008 12:05 AM
  •  Duane in Japan wrote:

     

    Me.Text = file.FullName


    May I please ask the purpose of this line of code and why is it not in the .jpg portion of the For Each/Next code, thanks to Martin.

     

    Sorry, this line of code "Me.Text = file.FullName" is redundant. Please ignore it.

     

    Hi Duane, I'm glad to see your recent passional participation in MSDN forums.

    Friday, January 18, 2008 1:46 AM
  • Martin, are the timer necessary?  - don't much about i just start

    Friday, January 18, 2008 3:12 AM
  •  augevonaar wrote:

    Martin, are the timer necessary?  - don't much about i just start

     

    Yes, one Timer control is required.

    It can automatically trigger the Timer_Tick event at specific interval, thus you can do something you expect in the Timer_Tick event.

    Friday, January 18, 2008 4:04 AM