image looping problem

Hello,

I am trying to loop through some images in a folder, have them displayed for a period of time based on the first two numbers of the name (e.g. 10birds.jpeg = 10 seconds for this image). Then when we reach the end of the folder we start again.

Sub ShowPics()

Dim i

Dim strPathAsString = Application.StartupPath & "\Images\Pic"

Dim oDirInfoAs DirectoryInfo =New DirectoryInfo(strPath)'Sets path to Folder

Dim oFiles()As FileInfo = oDirInfo.GetFiles'Gets File list from Folder

Dim oFileAs FileInfo

'timer1.Interval = 5000

'not sure how to pass the interval in

dim timeToRun

ForEach oFileIn oDirInfo.GetFiles

Path = strPath & "\" & oFile.Name.ToString

'invoke timer controll ? somehow

timeToRun=microsoft.visualbasic.left(ofile.Name,2)

' timer1.Start()

Next

IfNot (PictureBox.ImageIsNothing)Then PictureBox.Image.Dispose()

Dim CurrentFileAsString = Path

PictureBox.Image = Image.FromFile(CurrentFile)

' AddHandler timer1.Elapsed, AddressOf Me.OnTimerElapsed

'MsgBox(oFile.Name.ToString)

Next

EndSub

Sub OnTimerElapsed(ByVal senderAsObject, _

ByVal eAs System.Timers.ElapsedEventArgs)

Try

IfNot (PictureBox.ImageIsNothing)Then PictureBox.Image.Dispose()

Dim CurrentFileAsString = path

PictureBox.Image = Image.FromFile(CurrentFile)

Catch exAs Exception

EndTry

EndSub

I think i lost my self somewhere

Any guidance would be appreciated

Michael

[4750 byte] By [michaelp] at [2007-12-22]
# 1

Try Something along these lines:

Dim TimeUp As Boolean = True

Sub ShowPics()

Dim strPath As String = Application.StartupPath & "\Images\Pic"

Dim oDirInfo As DirectoryInfo = New DirectoryInfo(strPath) 'Sets path to Folder

Dim oFiles() As FileInfo = oDirInfo.GetFiles 'Gets File list from Folder

Dim oFile As FileInfo

Dim Path As String = String.Empty

'not sure how to pass the interval in

Dim timeToRun As Integer

For Each oFile In oDirInfo.GetFiles

TimeUp = True

Path = strPath & "\" & oFile.Name.ToString

timeToRun = CInt(Microsoft.VisualBasic.Left(oFile.Name, 2)) * 1000

PictureBox.Image = Image.FromFile(Path)

Timer1.Interval = timeToRun

Timer1.Start()

Do While Not TimeUp

DoEvents()

Loop

Next

End Sub

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

TimeUp = False

End Sub

That will get you through the directory of images 1 time...also you need to put some traps in place for checking that you have a valid file with a valid time...lots of error checking needs to be put in place an you will also have to set up you recursive procedure to go through the files again!

DMan1 at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

Here is an example form that does the slideshow per your specs:

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

Hope that helps!

rkimble at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

hi kimble,

your code looks interesting, I am actually trying it but .......

I am not getting the IO.SearchOption to work. I dunno if its because i am usng VS2003, framework 1.1

Now.Subtract(Me.LastUpdate) >= CurInt

operator >= not defined for timespan

any help

Thanks

MP

michaelp at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4

Yeah, that's the problem. Sorry, I should have posted a version that works in either framework version...

Make the following changes to correct the error:

1. Change Private CurInt As TimeSpan to Private CurInt as Integer

2. In GetNextImage() set CurInt = de.Value

3. Change the comaprison to check the TotalSeconds:
If Now.Subtract(Me.LastUpdate).TotalSeconds >= CurInt Then

That should fix the problem!

-EDIT-

Sorry, forgot about the IO.SearchOption.... Yeah, that doesn't work in 1.1 either. Just drop that third arguement. If you want to include subdirectories, write a little recursive function to get the list of files. You will probably replace that path with the folder that contains your images anyway - that was just so the example would run out of My Pictures for anyone else who might need this code.

rkimble at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 5

Hi Kimble

Thanks for the quick response will try it in a few

I changed

Dim files() As String = System.IO.Directory.GetFiles(path, "*.jpg") ', IO.SearchOption.AllDirectories)

Commenting out the alldirectories as there are non in the path so i hope that works too

Will let u know

Thanks

Michael

michaelp at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 6

Well I am amazed

Thanks Kimble worked like a charm except now i got to figure out how to get my other app into this one as i posted in http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=539362&SiteID=1

This still is very buggy. but thanks again.

And thanks to you too Dman1

michaelp at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...