-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainWindow.xaml.cs
94 lines (82 loc) · 2.3 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using ImageLibrary;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
namespace SlideShow;
public partial class MainWindow : Window
{
private List<string> images = new();
private int currentIndex = 0;
private readonly DispatcherTimer timer;
public MainWindow()
{
InitializeComponent();
timer = new DispatcherTimer
{
Interval = new System.TimeSpan(0, 0, 10)
};
timer.Tick += (s, e) => ShowNextImage();
Loaded += (s, e) =>
{
var location = @"C:\Users\jerem\Pictures\Idle Album";
// Uncomment this section if you want a folder dialog;
// otherwise, manually set the 'location' above.
var dlg = new FolderBrowserDialog { SelectedPath = location };
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
location = dlg.SelectedPath;
}
images = ImageLocator.GetImagesFromLocation(location);
images.Shuffle();
ShowImage(0);
timer.Start();
};
}
private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
switch (e.Key)
{
case Key.Space:
Pause();
break;
case Key.Next:
case Key.Right:
case Key.Enter:
ShowNextImage();
break;
case Key.Left:
case Key.PageUp:
ShowPreviousImage();
break;
case Key.Escape:
Close();
break;
default:
break;
}
}
private void Pause()
{
if (timer.IsEnabled)
timer.Stop();
else
timer.Start();
}
private void ShowImage(int index)
{
var image = Image.FromFile(images[index]) as Bitmap;
var imageSource = image?.ToWpfBitmap();
targetImage.Source = imageSource;
}
private void ShowNextImage()
{
currentIndex++;
if (currentIndex >= images.Count) currentIndex = 0;
ShowImage(currentIndex);
}
private void ShowPreviousImage()
{
if (currentIndex > 0) currentIndex--;
ShowImage(currentIndex);
}
}