Public Class Form1
'Create variable to hold the current display interval
Private CurInt As TimeSpan
'Create variable to hold last update time
Private LastUpdate As DateTime
'Create variable to hold list of image files and intervals
Private ImgFiles As New ArrayList
'Create variable to hold the index of current image
Private CurFileIdx As Integer = -1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Start the slide show
Me.StartSlideShow(System.Environment.GetFolderPath(Environment.SpecialFolder.MyPictures))
End Sub
'Load image info
Private Sub LoadImages(ByVal path As String)
'Empty the list of any current files
Me.ImgFiles.Clear()
'Get all JPEG files in the path and subfolders
Dim files() As String = System.IO.Directory.GetFiles(path, "*.jpg", IO.SearchOption.AllDirectories)
'Loop through each file
For Each f As String In files
'Create a default interval for this image in case
'the file name does not start with an interval
Dim imgint As Integer = 10
'Get the first two characters to test
Dim tst As String = f.Substring(0, 2)
'Test to see if the first two chars are numbers
If Char.IsNumber(tst, 0) And Char.IsNumber(tst, 1) Then
'If so, use that value as the interval
imgint = Integer.Parse(tst)
End If
'Create a dictionary entry to store the path and
'interval and add this to the list of image files
Me.ImgFiles.Add(New DictionaryEntry(f, imgint))
Next
'Reset the current image index
Me.CurFileIdx = -1
End Sub
Private Sub GetNextImage()
'Increment the current image index
Me.CurFileIdx += 1
'If the index exceeds the image list size,
'reset it to zero
If Me.CurFileIdx > Me.ImgFiles.Count - 1 Then
Me.CurFileIdx = 0
End If
'Dispose of any current image
If Not PictureBox1.Image Is Nothing Then
PictureBox1.Image.Dispose()
End If
'Make sure the image list contains images
If Me.ImgFiles.Count > 0 Then
'Get the current dictionary entry
Dim de As DictionaryEntry = Me.ImgFiles(Me.CurFileIdx)
'Set the interval for this image
Me.CurInt = New TimeSpan(0, 0, de.Value)
'Set the last update time
Me.LastUpdate = Now
'Load the image
PictureBox1.Image = New Bitmap(de.Key.ToString)
Else
'Throw up a little warning and stop the slideshow
'if there are no images in the image list
Me.HeartBeatTimer.Stop()
MessageBox.Show("No images loaded")
End If
End Sub
Private Sub HeartBeatTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HeartBeatTimer.Tick
'Check once per second to see if the current
'interval time has elapsed
If Now.Subtract(Me.LastUpdate) >= CurInt Then
'If so, get the next image
Me.GetNextImage()
End If
End Sub
'Define a little routine to kick things off
Public Sub StartSlideShow(ByVal picpath As String)
Me.LoadImages(picpath)
Me.GetNextImage()
Me.HeartBeatTimer.Start()
End Sub
End Class