3
Answers

I want to make a simple picture slideshow from the stated fo

Photo of tan shen

tan shen

7y
158
1
I want to make a simple picture slideshow from the stated folder. The label show the full location detail of the image, how can the label show the picture name only?
  1. int counter = 0;  
  2. private void button1_Click(object sender, EventArgs e)  
  3. {  
  4. timer1.Start(); //start the slideshow  
  5. }  
  6. private void timer1_Tick(object sender, EventArgs e)  
  7. {  
  8. counter++;  
  9. string path = @"C:\Users\Tan Wei Shen\Desktop\FYP\Animal";//directory of the picture  
  10. string[] filePaths = Directory.GetFiles(path, "*.jpg ");  
  11. if (counter > filePaths.Length - 1)  
  12. {  
  13. counter = 0; //reset the picture  
  14. }  
  15. pictureBox1.Image = Image.FromFile(filePaths[counter]); //show image  
  16. label1.Text = filePaths[counter]; //show name of the image  
  17. }

Answers (3)

0
Photo of Bryian Tan
NA 9.4k 887.4k 7y
You can use regular expression to get the file name. Here is an example: https://dotnetfiddle.net/DdzA3c
 
  1. using System;  
  2. using System.Text.RegularExpressions;  
  3.                       
  4. public class Program  
  5. {  
  6.     public static void Main()  
  7.     {  
  8.         string file = @"C:\Users\xyz\Desktop\FYP\Animal\abc.jpg";  
  9.         string pattern  = @"[ \w-]+?(?=\.)";  
  10.           
  11.         Regex regex = new Regex(pattern);  
  12.           
  13.         Match match = regex.Match(file);  
  14.           
  15.         Console.WriteLine(match.Value);  
  16.     }  


0
Photo of Nilesh Patil
NA 3.4k 7.1k 7y
Hi,
 
Refer This Code
 
private void timer1_Tick(object sender, EventArgs e)
{
counter++;
try // Get the tif file from C:\image\ folder
{
string path = @"C:\image\";
String filename = Directory.EnumerateFiles(path, "*.png").FirstOrDefault();
if (null != filename) {
// Load picture
pictureBox1.Load(filename);
// Show the file name
lblFile.Text = filename;
}
else {
//TODO: No *.png files are found
}
}
catch(IOException ex)
{
MessageBox.Show("No files or " + ex.Message);
}
}
 
0
Photo of Bryian Tan
NA 9.4k 887.4k 7y
and your question is?