Hello,
May be this will help you.
change the code as per your need...
I used Background worker (not timer)
PictureBox control name: picturebox1
Progress Bar Control name: progressbar1
first of all
create variables.
DirectoryInfo di;
List<FileInfo> images= new List<FileInfo>();
string dirpath = @"C:\Images";
int timeTemp = 0;
BackgroundWorker backgroundWorker = new BackgroundWorker
{
WorkerReportsProgress = true,
WorkerSupportsCancellation = true
};
In Form Load event
di = new DirectoryInfo(dirpath);
GetImagesOfType("*.jpg");
backgroundWorker.DoWork += BackgroundWorkerOnDoWork;
backgroundWorker.ProgressChanged += BackgroundWorkerOnProgressChanged;
ChangePicture();
if (!backgroundWorker.IsBusy)
backgroundWorker.RunWorkerAsync();
Add below events for Background Worker
private void BackgroundWorkerOnProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
private void BackgroundWorkerOnDoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = (BackgroundWorker)sender;
while (!worker.CancellationPending)
{
Thread.Sleep(1000);
if (timeTemp < 100) timeTemp += 10;
else
{
timeTemp = 0;
ChangePicture();
}
worker.ReportProgress(timeTemp);
}
}
Change Picture Method
public void ChangePicture()
{
Random d = new Random();
int index = d.Next(0, images.Count);
FileInfo item = images.ElementAt(index);
pictureBox1.Image = Image.FromFile(item.FullName);
}
Get Images Method
public void GetImagesOfType(string type)
{
FileInfo[] Img = di.GetFiles(type);
foreach (FileInfo im in Img)
images.Add(im);
}