I would like that when the form loads and/or starts my picture slide will start automatically.I tried to put the path of where the folder is located but it keeps giving an error. When I use it a dialog box it works. I am trying to bypass the dialog box so it starts automatically.
public partial class Form1 : Form
{
private string[] folderFile = null;
private int selected = 0;
private int end = 0;
FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
// The folder is pre created
string path1 = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\Pictures";
public Form1()
{
InitializeComponent();
//This does not work when the form starts up.
if (!Directory.Exists(path1))
{
string[] part1 = null, part2 = null, part3 = null;
part1 = Directory.GetFiles(path1, "*.jpg");
part2 = Directory.GetFiles(path1, "*.jpeg");
part3 = Directory.GetFiles(path1, "*.bmp");
folderFile = new string[part1.Length + part2.Length + part3.Length];
Array.Copy(part1, 0, folderFile, 0, part1.Length);
Array.Copy(part2, 0, folderFile, part1.Length, part2.Length);
Array.Copy(part3, 0, folderFile, part1.Length + part2.Length, part3.Length);
selected = 0;
//begin = 0;
end = folderFile.Length;
showImage(folderFile[selected]);
// 5 to 10 second intervals
//timer1.Enabled = true;
}
else
{
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
nextImage();
}
private void btnFolder_Click(object sender, EventArgs e)
{
//Original
//This works!!
//while (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
//{
// string[] part1 = null, part2 = null, part3 = null;
// part1 = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.jpg");
// part2 = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.jpeg");
// part3 = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.bmp");
// folderFile = new string[part1.Length + part2.Length + part3.Length];
// Array.Copy(part1, 0, folderFile, 0, part1.Length);
// Array.Copy(part2, 0, folderFile, part1.Length, part2.Length);
// Array.Copy(part3, 0, folderFile, part1.Length + part2.Length, part3.Length);
// selected = 0;
// //begin = 0;
// end = folderFile.Length;
// showImage(folderFile[selected]);
// //btnPrev.Enabled = true;
// //btnNext.Enabled = true;
// //btnStartSlide.Enabled = true;
//}
}
private void showImage(string path)
{
Image imgtemp = Image.FromFile(path);
//pictureBox1.Width = imgtemp.Width / 2;
//pictureBox1.Height = imgtemp.Height / 2;
//pictureBox1.Image = imgtemp;
panel1.BackgroundImage = imgtemp;
}
private void prevImage()
{
if (selected == 0)
{
selected = folderFile.Length - 1;
showImage(folderFile[selected]);
}
else
{
selected = selected - 1;
showImage(folderFile[selected]);
}
}
private void nextImage()
{
if (selected == folderFile.Length - 1)
{
selected = 0;
showImage(folderFile[selected]);
}
else
{
selected = selected + 1;
showImage(folderFile[selected]);
}
}
private void btnPreviews_Click(object sender, EventArgs e)
{
prevImage();
}
private void btnNext_Click(object sender, EventArgs e)
{
nextImage();
}
private void btnStart_Click(object sender, EventArgs e)
{
if (timer1.Enabled == true)
{
timer1.Enabled = false;
btnStart.Text = "<< START >>";
}
else
{
timer1.Enabled = true;
btnStart.Text = "<< STOP >>";
}
}
}
}
Try
string path1 = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "\\Sample_Pictures";
or
string path1 = Environment.GetFolderPath(Environment.SpecialFolder.CommonPictures) + "\\Sample_Pictures";
Or use
string publicDesktopPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory);
var directory = new DirectoryInfo(publicDesktopPath);
string path1 = directory.Parent.FullName + "\\Pictures\\Sample_Pictures";
and fix your conditional
if (!Directory.Exists(path1)) {
to
if (Directory.Exists(path1)) {
so that you don't try operations on an non-existent directory.
To get it to cycle through your pictures, you could use a System.Timers.Timer:
In your Form1 class
private static Timer timer;
Declare the timer in your constructor:
timer = new System.Timers.Timer(5000); // change interval in milliseconds
timer.Elapsed += OnTimedEvent;
timer.Enabled = true;
Create the OnTimedEvent method in your Form1 class:
private static void OnTimedEvent(Object source, ElapsedEventArgs e)
{
// Do what you want every time the timer elapses that interval
nextImage();
}
Related
I try to make my software display a green or red square depending on the ip address whether it is valid or not, of course all its in a FlowLayoutPanel (ca_imp) and before I create a square number based on the number of addresses I'll add in a second FlowLayoutPanel(listeImprimantes).
private void RdFichierXml()
{
int i = 0;
XmlDocument xmlDoc = new XmlDocument(); // Create an XML document object
xmlDoc.Load("imprimante.xml"); // Load the XML document from the specified file
// Get elements
girlNom = xmlDoc.GetElementsByTagName("nom");
girlIp = xmlDoc.GetElementsByTagName("ip");
girlRemarques = xmlDoc.GetElementsByTagName("remarques");
// Display the results
for (i = 0; i < girlIp.Count; i++)
{
buttons(girlIp[i].InnerText, girlNom[i].InnerText, girlRemarques[i].InnerText);
buttons1(i, girlIp[i].InnerText);
}
}
private void buttons(string ip, string name, string remarque)
{
Panel Case = new Panel();
Case.Font = new System.Drawing.Font("Mont", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
Case.Name = "Case";
Case.Size = new System.Drawing.Size(234, 49);
Case.Text = name + "\r\nIP : " + ip + "\r\nREMARQUE : " + remarque + "\r\n"; ;
Case.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
listeImprimantes.Controls.Add(Case);
}
private void buttons1(int i, string ip)
{
Panel Case1 = new Panel();
Case1.Name = "Case_color" + i;
Case1.Size = new System.Drawing.Size(16, 49);
Case1.TabIndex = i;
Case1.BringToFront();
Case1.BackColor = Color.Gray;
ca_imp.Controls.Add(Case1);
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < girlIp.Count; i++)
{
/*if (check(girlIp[i].InnerText) == 1) //check is a funtion that return 1 if the ip is valid or 0 if not
{
?.BackColor = Color.Green;
}
else if (check(girlIp[i].InnerText) == 0)
{
?.BackColor = Color.Red;
}*/
}
}
Can i get some help for change the color with the backgroundWorker or other without freeze my window please...
There are many ways to do this, here's one:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < girlIp.Count; i++)
{
int result = check(girlIp[i].InnerText);
String cltName = "Case_color" + i;
Panel pnl = this.Controls.Find(ctlName, true).FirstOrDefault() as Panel;
if (pnl != null)
{
pnl.Invoke((MethodInvoker)delegate () {
pnl.BackColor = (result == 1) ? Color.Green : Color.Red;
});
}
}
}
I am using the events ItemSelectionChanged and SelectedIndexChanged of the ListView class to answer this question.
The code is below, and the only bug that seems to remain is that I use the 250ms timer also when the user uses rubberband selection, not only for single or double click selection.
internal Form1()
{
InitializeComponent();
t.Tick += T_Tick;
}
internal bool santinela = false;
internal void T_Tick(object sender, EventArgs e)
{
t.Stop();
if (!santinela)
{
bool newVal = selectionChangedItem.Selected;
selectionChangedItem.Checked =
selectionChangedItem.Selected = newVal;
santinela = true;
}
}
internal Timer t = new Timer()
{
Interval = 250
};
internal bool santinela3 = true;
internal void listView1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (santinela3)
{
if (e.CurrentValue == CheckState.Checked)
{
listView1.SelectedIndices.Remove(e.Index);
}
else if (e.CurrentValue == CheckState.Unchecked)
{
listView1.SelectedIndices.Add(e.Index);
}
}
}
internal void listView1_ItemActivate(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
MessageBox.Show(listView1.SelectedItems[0].Text);
}
}
internal ListViewItem selectionChangedItem = null;
internal bool santinela2 = true;
internal void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
if (!santinela2)
{
return;
}
if (t.Enabled)
{
santinela = true;
t.Stop();
// double click: both clicks must be done in the same place
if (e.Item == selectionChangedItem)
{
if (!e.IsSelected)
{
santinela2 = true;
e.Item.Selected = true;
santinela2 = false;
}
listView1_ItemActivate(sender, EventArgs.Empty);
}
selectionChangedItem = null;
}
else
{
santinela = false;
t.Stop();
selectionChangedItem = e.Item;
t.Start();
}
}
internal void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedIndices.Count == 0)
{
santinela = true;
t.Stop();
selectionChangedItem = null;
santinela3 = false;
for (int i = 0; i < listView1.CheckedItems.Count; ++i)
{
listView1.CheckedItems[i].Checked = false;
}
santinela3 = true;
}
}
The relevant designer code is below:
System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem("item1");
System.Windows.Forms.ListViewItem listViewItem2 = new System.Windows.Forms.ListViewItem("item2");
this.listView1 = new System.Windows.Forms.ListView();
this.SuspendLayout();
//
// listView1
//
this.listView1.CheckBoxes = true;
listViewItem1.StateImageIndex = 0;
listViewItem2.StateImageIndex = 0;
this.listView1.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
listViewItem1,
listViewItem2});
this.listView1.Location = new System.Drawing.Point(12, 12);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(401, 327);
this.listView1.TabIndex = 0;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.ItemActivate += new System.EventHandler(this.listView1_ItemActivate);
this.listView1.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.listView1_ItemCheck);
this.listView1.ItemSelectionChanged += new System.Windows.Forms.ListViewItemSelectionChangedEventHandler(this.listView1_ItemSelectionChanged);
this.listView1.SelectedIndexChanged += new System.EventHandler(this.listView1_SelectedIndexChanged);
Update: The issue I am facing because of the timer is that after hovering with the rubberband a ListViewItem, there is the useless delay of the timer before the item gets checked. When the user resizes/moves the rubberband so that the ListViewItem is no longer checked, there is the same delay. If the user does not know of that non-standard delay, the selection can be wrong.
private int FilesNamesCounter = 0;
private String FileName = "";
private List<string> myGifs = new List<string>();
private bool cancelop = false;
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker bgw = (BackgroundWorker)sender;
int Counter = 0;
int percentage = 0;
int total = allfiles.Count;
for (int i = 0; i < allfiles.Count; i++)
{
if (bgw.CancellationPending == true)
{
pictureBox1.Image.Dispose();
pictureBox1.Image = Properties.Resources.Weather_Michmoret;
e.Cancel = true;
makeGif = false;
cancelop = true;
timer1.Stop();
break;
}
else
{
timer1.Start();
Counter += 1;
// calculating percentage and report it
percentage = Counter * 100 / total;
bgw.ReportProgress(percentage);
converttogif();
}
}
if (makeGif == true)
{
FilesNamesCounter += 1;
unfreez.MakeGIF(myGifs, outputfile + FilesNamesCounter + ".gif", 80, true);
}
bgw.ReportProgress(100);
e.Result = allfiles;
}
Convertion method:
private void converttogif()
{
DirectoryInfo dirinfo = new DirectoryInfo(selectedfilesdirectoryName);
FileInfo[] gifFileInfo = dirinfo.GetFiles("*.gif");
Image gifImage;
for (int i = 0; i < allfiles.Count; i++)
{
FileName = allfiles[i];
gifImage = Image.FromFile(gifFileInfo[i].FullName);
gifImage.Save(FileName, System.Drawing.Imaging.ImageFormat.Gif);
gifImage.Dispose();
myGifs.Add(FileName);
}
}
Progress Changed event:
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
lblStatus.Text = "Processing......" + progressBar1.Value.ToString() + "%";
}
Completed event:
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if ((e.Cancelled == true))
{
button2.Enabled = false;
lblStatus.Text = "Task Cancelled.";
}
else if (!(e.Error == null))
{
lblStatus.Text = "Error while performing background operation.";
}
else
{
lblStatus.Text = "Task Completed...";
allfiles = (List<string>)e.Result;
timer1.Stop();
button1.Enabled = false;
progressBar1.BackColor = Color.FromArgb(0, 211, 040);
displaylastanimatedgif();
}
}
The problem is with the converttogif method in the DoWork event.
It will do loop in loop and will make each time the loop in the converttogif over and over again since i'm doing a loop over the images in the dowork already.
What i want to do is to report the progress in percentages of the convertion progress to the progressBar1. If i have in allfiles for example 285 files image to convert then convert them once and report on each file convert the progress in percentage to the progressBar so the whole convertion should be in the progressBar start from 0% to 100% or maybe it's logical to start from 1% to 100%.
But the idea to report and show the convertion progress.
UPDATE
This is what i tried now:
private int FilesNamesCounter = 0;
private String FileName = "";
private List<string> myGifs = new List<string>();
private bool cancelop = false;
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker bgw = (BackgroundWorker)sender;
converttogif(bgw);
bgw.ReportProgress(100);
e.Result = allfiles;
}
private void converttogif(BackgroundWorker bgw)
{
int Counter = 0;
int percentage = 0;
int total = allfiles.Count;
DirectoryInfo dirinfo = new DirectoryInfo(selectedfilesdirectoryName);
FileInfo[] gifFileInfo = dirinfo.GetFiles("*.gif");
Image gifImage;
for (int i = 0; i < allfiles.Count; i++)
{
FileName = allfiles[i];
gifImage = Image.FromFile(gifFileInfo[i].FullName);
gifImage.Save(FileName, System.Drawing.Imaging.ImageFormat.Gif);
gifImage.Dispose();
myGifs.Add(FileName);
Counter += 1;
// calculating percentage and report it
percentage = Counter * 100 / total;
bgw.ReportProgress(percentage);
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
lblStatus.Text = "Processing......" + progressBar1.Value.ToString() + "%";
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if ((e.Cancelled == true))
{
button2.Enabled = false;
lblStatus.Text = "Task Cancelled.";
}
else if (!(e.Error == null))
{
lblStatus.Text = "Error while performing background operation.";
}
else
{
if (makeGif == true)
{
FilesNamesCounter += 1;
unfreez.MakeGIF(myGifs, outputfile + FilesNamesCounter + ".gif", 80, true);
}
lblStatus.Text = "Task Completed...";
allfiles = (List<string>)e.Result;
timer1.Stop();
button1.Enabled = false;
progressBar1.BackColor = Color.FromArgb(0, 211, 040);
displaylastanimatedgif();
}
}
The problem now is that it's getting to 99% and much before the end of the progressBar like 3/4 of the way but show 99% then it's waiting i used a breakpoint and it's getting to the completed event creating the animated gif then show 100% and the progressBar getting to the end.
How can i fix it ?
Easy, but not nice, way: pass the background worker to your converttogif function, and call bgw.ReportProgress(percentage); within the cicle.
A better solution is having your converttoGif be a part of a different class, raising its own "progress" event you can register to, and in the event handler call the bgw.ReportProgress
This would keep the gifconversion code isolated from the form and its backgroudworker, leaving the form the need to register for conversion class events.
Here I have a list[N] of images, two picturebox, from time to time shows the next image,like the auto play album, and the interval time should be different, anybody can help:) thanks a lot
try
{
for (int index = 0; index < list.Count; index++)
{
aTimer = new System.Timers.Timer(10000);
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = Convert.ToInt64(arrTime[index]);
aTimer.Enabled = true;
pictureBox1.Image = list[index];
pictureBox2.Image = list[index+1];
}
}
catch
{
MessageBox.Show(e.ToString());
}
and what to do with the Timer_Tick(){}...
If you want the two PictureBoxes to change at the same time, then do something like:
public partial class Form1 : Form
{
private Random R = new Random();
private IEnumerator<Image> images;
private List<Image> list = new List<Image>();
private System.Windows.Forms.Timer aTimer = new System.Windows.Forms.Timer();
public Form1()
{
InitializeComponent();
this.Load += Form1_Load;
}
private void Form1_Load(object sender, EventArgs e)
{
// ...populate "list" somehow ...
String PicturesPath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
foreach(String PictureFile in System.IO.Directory.GetFiles(PicturesPath, #"*.png"))
{
list.Add(new Bitmap(PictureFile));
}
aTimer.Interval = R.Next(3000, 10001); // 3 to 10 second interval
aTimer.Tick += aTimer_Tick;
aTimer.Start();
ChangeImages();
}
private void ChangeImages()
{
pictureBox1.Image = NextImage();
pictureBox2.Image = NextImage();
}
private Image NextImage()
{
if (images == null && list.Count > 0)
{
images = list.GetEnumerator();
}
if (images != null)
{
if (!images.MoveNext())
{
images.Reset();
images.MoveNext();
}
return images.Current;
}
else
{
return null;
}
}
private void aTimer_Tick(object sender, EventArgs e)
{
// change the Interval:
aTimer.Interval = R.Next(3000, 10001); // 3 to 10 second interval
ChangeImages();
}
}
If you want the two PictureBoxes to change independently, then use two Timers and change them separately:
public partial class Form1 : Form
{
private Random R = new Random();
private IEnumerator<Image> images;
private List<Image> list = new List<Image>();
private System.Windows.Forms.Timer aTimer1 = new System.Windows.Forms.Timer();
private System.Windows.Forms.Timer aTimer2 = new System.Windows.Forms.Timer();
public Form1()
{
InitializeComponent();
this.Load += Form1_Load;
}
private void Form1_Load(object sender, EventArgs e)
{
// ...populate "list" somehow ...
String PicturesPath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
foreach(String PictureFile in System.IO.Directory.GetFiles(PicturesPath, #"*.png"))
{
list.Add(new Bitmap(PictureFile));
}
aTimer1.Interval = R.Next(3000, 10001); // 3 to 10 second interval
aTimer1.Tick += aTimer1_Tick;
aTimer1.Start();
aTimer2.Interval = R.Next(3000, 10001); // 3 to 10 second interval
aTimer2.Tick += aTimer2_Tick;
aTimer2.Start();
pictureBox1.Image = NextImage();
pictureBox2.Image = NextImage();
}
private Image NextImage()
{
if (images == null && list.Count > 0)
{
images = list.GetEnumerator();
}
if (images != null)
{
if (!images.MoveNext())
{
images.Reset();
images.MoveNext();
}
return images.Current;
}
else
{
return null;
}
}
private void aTimer1_Tick(object sender, EventArgs e)
{
// change the Interval:
aTimer1.Interval = R.Next(3000, 10001); // 3 to 10 second interval
pictureBox1.Image = NextImage();
}
private void aTimer2_Tick(object sender, EventArgs e)
{
// change the Interval:
aTimer2.Interval = R.Next(3000, 10001); // 3 to 10 second interval
pictureBox2.Image = NextImage();
}
}
try
{
for (int index = 0; index < fileNum / 2 - 1; index++)
{
aTimer = new System.Timers.Timer(10000);
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = Convert.ToInt64(arrTime[index]);
aTimer.Enabled = true;
pictureBox1.Image = list[index];
pictureBox2.Image = list[index+1];
}
}
catch
{
MessageBox.Show(e.ToString());
}
I'm trying to make a slideshow with a mediaElement that shows each image in listbox x seconds.
How do I make my code play each image x seconds before continuing?
This code adds all images to a listbox named Listbox1
Dictionary<string, string> Listbox1Dict = new Dictionary<string, string>();
private void SearchBtn_Click(object sender, RoutedEventArgs e)
{
Listbox1.Items.Clear();
FolderBrowserDialog folderDialog = new FolderBrowserDialog();
folderDialog.SelectedPath = "C:\\";
DialogResult result = folderDialog.ShowDialog();
if (result.ToString() == "OK")
FileNameTextBox.Text = folderDialog.SelectedPath;
string directory = FileNameTextBox.Text;
var files = Directory.GetFiles(directory).Where(name => !name.EndsWith(".ini"));
foreach (string file in files)
{
Listbox1.Items.Add(System.IO.Path.GetFileNameWithoutExtension(file));
Listbox1Dict.Add(System.IO.Path.GetFileNameWithoutExtension(file), file);
}
}
This code shows all images in fullscreen but it skips everyone to last image at start.
private void button1_Click_1(object sender, RoutedEventArgs e)
{
foreach (var selected in Listbox1.Items)
{
string s = selected.ToString();
if (Listbox1Dict.ContainsKey(s))
{
mediaElement1.Visibility = Visibility.Visible;
SearchBtn.Visibility = Visibility.Hidden;
Listbox1.Visibility = Visibility.Hidden;
FileNameTextBox.Visibility = Visibility.Hidden;
mediaElement1.Source = new Uri(Listbox1Dict[s]);
mediaElement1.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
mediaElement1.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
this.Background = new SolidColorBrush(Colors.Black);
this.WindowStyle = WindowStyle.None;
this.WindowState = WindowState.Maximized;
}
}
}
Tried this code to make the image play one by one but I get an error. Look on comment on code:
private int currentSongIndex = -1;
void mediaElement1next(object sender, EventArgs e)
{
if(currentSongIndex == -1)
{
currentSongIndex = Listbox1.SelectedIndex;
}
currentSongIndex++;
if(currentSongIndex < Listbox1.Items.Count)
{
mediaElement1.Play(Listbox1.Items[currentSongIndex]); // No overload for method 'Play' takes 1 arguments
}
else
{
// last song in listbox has been played
}
}
I think you need a timer to set your next image. Using the code you're currently using, it will iterate through your list and change the image until you get to the end.
Take a look at DispatcherTimer. You could set it so, at each tick, it would change to the next image. Something like this (just writing off my head)
dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
Then, inside your eventhandler:
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
// get the next image
}
Of course you can use other kinds of timers, but that's the main idea.
Hold your image paths in a list & use the tick event of a timer.
Something like:
List<string> paths = new List<string>();
private void timer1_Tick(object sender, EventArgs e)
{
pictureBox1.Image = getNextImage();
}
private string getNextImage()
{
//code...
}
enter code here
EDIT:
Add a class variable: int index = 0;
On the SearchBtn_Click event, add the results to the list.
//..
foreach (string file in files)
{
paths.Add(file);
}
//..
Then do as I did above and the content of the getNextImage method would be:
private string getNextImage()
{
if(index < paths.Count - 1)
{
index += 1;
}
else
{
index = 0;
}
return paths[index];
}
My idea would be to implement a thread that counted to X and then called a NextImage() function when done.
Something like this:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
if (Listbox1.Items.Count > 0)
{
if (dispatcherTimer.IsEnabled)
dispatcherTimer.Stop();
else
{
curImage = 0;
dispatcherTimer.Start();
}
}
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
Dispatcher.Invoke((Action)delegate
{
ShowNextImage();
}, null);
}
private void ShowNextImage()
{
if (curImage >= Listbox1.Items.Count)
curImage = 0;
var selected = Listbox1.Items[curImage];
string s = selected.ToString();
if (Listbox1Dict.ContainsKey(s))
{
mediaElement1.Visibility = Visibility.Visible;
SearchBtn.Visibility = Visibility.Hidden;
Listbox1.Visibility = Visibility.Hidden;
FileNameTextBox.Visibility = Visibility.Hidden;
mediaElement1.Source = new Uri(Listbox1Dict[s]);
mediaElement1.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
mediaElement1.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
this.Background = new SolidColorBrush(Colors.Black);
this.WindowStyle = WindowStyle.None;
this.WindowState = WindowState.Maximized;
}
}
and declaration
DispatcherTimer dispatcherTimer = new DispatcherTimer();
int x = 2; //seconds
private int curImage = 0;
and some construct
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, x);