What would be the best and easiest way to change background image in a VB application from images that are preselected by user from any given folder on the hard disk.
So say "Mike" selects some images in D:\Images\Nature in the Nature folder we have lets say "20" images. No开发者_Python百科w the VB app reads this and saves path so next time its open its fetching images from that folder once more. And every few sec lets say 45 sec a new image is loaded for background.
When the user selects a picture folder, store the folder in the registry. When the program loads, fetch the folder from the registry. You can save it like this:
SaveSetting ("mayappname","settings","picturefolder",PicFolderName)
and restore the folder when the program loads with:
Dim PicFolderName As String = GetSetting("mayappname", "settings", "picturefolder", My.Computer.FileSystem.SpecialDirectories.MyDocuments)
This example is reading ALL jpg-images in the folder and change image every 45 seconds to next image in the folder. If you want the user to be able to select 20 images and rotate between them, let me know, then we have to store every picture the user selected and rotate between them only. But this code rotate between all pictures in the selected folder.
I guess you know how to change it to just work with the pictures the user selected. I will not give that answer because it can be done in so many ways so I need more information in how you want the interactivity between the user and the program to be before I do that.
Well. If you have the code that store the folder in the registry, then paste this to the form to change picture every 45 seconds from the pictures in the selected folder:
Private ImageNames As New List(Of String)
Private ImageIndexNow As Integer = 0
Private PictureTimer As New Timer
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Get the saved path for where the images are stored.
Dim PicFolderName As String = GetSetting("mayappname", "settings", "picturefolder", My.Computer.FileSystem.SpecialDirectories.MyDocuments)
'PicFolderName="c:\mypictures" ' remove rem if you just want to test with a specific folder
'Call sub that read in all names of images in that path.
LoadImageNames(PicFolderName)
PictureTimer.Interval = 45000 '45 seconds
PictureTimer.Enabled = True
AddHandler PictureTimer.Tick, AddressOf PictureTimer_Tick
End Sub
Sub LoadImageNames(ByVal ImagePath As String)
'Load image names in a list of strings for the provided Imagepath
For Each file As String In IO.Directory.GetFiles(ImagePath , "*.jpg")
ImageNames.Add(file)
Next
End Sub
Private Sub PictureTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
'set background image of the form to image number imageIndexNow in the list of images
Me.BackgroundImageLayout = ImageLayout.Stretch
Me.BackgroundImage = Image.FromFile(ImageNames(ImageIndexNow))
ImageIndexNow += 1 ' Add one to the number so next picture is selected next time the timer-tick is fired.
If ImageIndexNow > ImageNames.Count-1 Then ImageIndexNow = 0 ' Start from zero if imageIndexNow is larger than amount of images.
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
RemoveHandler PictureTimer.Tick, AddressOf PictureTimer_Tick
End Sub
If you want to change this to only rotate between 20 userselected pictures, change the program to store those picture names in the ImageNames-list. And then store this list eather in registry, a file or database. When program loads, restore the list and you can use the code above with allmost no change at all.
精彩评论