开发者

WPF Slideshow and photo gallery

开发者 https://www.devze.com 2023-01-06 14:02 出处:网络
I want to create an image slideshow using WPF and C#, but I 开发者_StackOverflow中文版don`t know how to do this. I want to do it automatically (change picture after time) and also by buttons that user

I want to create an image slideshow using WPF and C#, but I 开发者_StackOverflow中文版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 ...


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消