sikas
sikas

Reputation: 5523

WPF Slideshow and photo gallery

I want to create an image slideshow using WPF and C#, but I don`t know how to do this. I want to do it automatically (change picture after time) and also by buttons that user can click on ...

Upvotes: 2

Views: 7140

Answers (1)

ChrisF
ChrisF

Reputation: 137158

One way is to have all your images in a folder and then use a timer to control the code that chooses one of these images. If you you want it to be random you could do something like this:

Random random = new Random();  // Only do this once

string[] images = Directory.GetFiles(root, "*.jpg");
string chosen = images[random.Next(0, images.Length)];

If you want sequential then just generate the list once, keep a note of the current position and then just increment it - taking care to roll back to 0 when you hit the end of the array.

In the main UI thread, handle the event and update an <Image> to display the image.

image.Source = new BitmapImage(new Uri(chosen, UriKind.Relative));

The next and previous buttons can just select the next and previous images in the folder.

Upvotes: 2

Related Questions