load last modified picture in a directory to picture box

rob_b

New member
Joined
Jun 22, 2014
Messages
2
Programming Experience
Beginner
hey,

trying to load a picture into a picture box (using vb.net) on form load however the picture has to be the last modified picture in the directory. Your thinking why?? i run media portal as my htpc and ive managed (with a little googling around) to make a pop up box that displays at the bottom right of the screen when ever the front door cctv camera detects motion. having no programming skills ive impressed myself however its not that clever all i is a form that loads for 5 seconds that has a text box that says motion detected.

so the situation is that the cctv software creates an alert snap shot jpeg of the detection. i'd like the form when loaded to pull this pic up into a box so i could see whats happening. the picture name changes on the time and date and would be hard to pre program it to pic the exact picture which most examples ive looked at do.

i would of liked to pull the rstp feed in live however i think that would be asking alot and pulling the latest pic in would be easier.

Anyway thanks in advance for anyone that can help me.
 
Last edited:
You can create a DirectoryInfo object for the folder, call GetFiles to create an array of FileInfo objects for the files in the folder, sort the FileInfo array by CreationTime or LastWriteTime, whichever is more appropriate, and then take the one with the latest time. You can then use that FileInfo to load the file in whatever way is most appropriate, e.g. Image.FromFile or PictureBox.Load.
Dim folder As New DirectoryInfo("folder path here")
Dim files = folder.GetFiles()
Dim latestFile = files.OrderBy(Function(fi) fi.CreationTime).LastOrDefault() 'Use LastOrDefault over Last in case there are no files at all.

If latestFile IsNot Nothing Then
    Me.PictureBox1.Load(latestFile.FullName)
End If
 
Brilliant! thank you so much :) it now imports the last created picture in the directory. Next problem........ how do i make the picture re-size down to the size of the picture box? thank you for your help
 
Brilliant! thank you so much :) it now imports the last created picture in the directory. Next problem........ how do i make the picture re-size down to the size of the picture box? thank you for your help

That's unrelated to the topic of this thread so should be asked in a new thread. in future, please keep each thread to a single topic and each topic to a single thread.

To answer your question, look at the SizeMode property of the PictureBox. I think Zoom is the option that will resize the Image to fit the control while retaining the original aspect ratio.
 
Back
Top