tags:

views:

32

answers:

1

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 ...

+2  A: 

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.

ChrisF
@ChrisF: How can I use threads with this piece of code? and for the sequence it doesn`t need to be random, I can just have it sequential int i=0; i = (i+1)%images.Length;
sikas
@Sikas - if it doesn't have to be random then your sequential code should be fine. I'll update the answer to be a bit clearer on the thread/timer issue.
ChrisF
@ChrisF: Thanks for the example, yet I want you to tell me how to work with threads as I don`t know anything about it. if you know any tutorial it`ll be great
sikas
@Sikas - For threading starting here - http://msdn.microsoft.com/en-us/library/1c9txz50(VS.71).aspx - is as good as anywhere. For an in depth discussion of timers check this post - http://msdn.microsoft.com/en-us/magazine/cc164015.aspx
ChrisF