-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindowViewModel.cs
More file actions
240 lines (216 loc) · 8.04 KB
/
MainWindowViewModel.cs
File metadata and controls
240 lines (216 loc) · 8.04 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using Accord.Video.FFMPEG;
using AForge.Video;
using AForge.Video.DirectShow;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
namespace VideoCapture
{
class MainWindowViewModel : ViewModelBase, IDisposable
{
public ObservableCollection<FilterInfo> VideoDevices { get; set; }
private BitmapImage image;
public BitmapImage Image { get => image; set => Set(ref image, value); }
private bool isDesktopSource;
public bool IsDesktopSource { get => isDesktopSource; set => Set(ref isDesktopSource, value); }
private bool isWebcamSource;
public bool IsWebcamSource { get => isWebcamSource; set => Set(ref isWebcamSource, value); }
private IVideoSource videoSource;
private VideoFileWriter writer;
private FilterInfo currentDevice;
public FilterInfo CurrentDevice { get => currentDevice; set => Set(ref currentDevice, value); }
private bool recording;
private DateTime? firstFrameTime;
public MainWindowViewModel()
{
VideoDevices = new ObservableCollection<FilterInfo>();
GetVideoDevices();
IsDesktopSource = true;
}
public void Dispose()
{
if (videoSource != null && videoSource.IsRunning)
videoSource.SignalToStop();
writer?.Dispose();
}
private void SaveSnapshot()
{
var dialog = new Microsoft.Win32.SaveFileDialog();
dialog.FileName = "Snapshot1";
dialog.DefaultExt = ".png";
var dialogResult = dialog.ShowDialog();
if (dialogResult != true)
{
return;
}
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(Image));
using (var fileStream = new FileStream(dialog.FileName, FileMode.Create))
//var FileName = "G:\\Snapshot.png";
//using (var fileStream = new FileStream(FileName, FileMode.Create))
{
encoder.Save(fileStream);
}
}
private void StartRecording()
{
var dialog = new Microsoft.Win32.SaveFileDialog();
dialog.FileName = "Video1";
dialog.DefaultExt = ".avi";
dialog.AddExtension = true;
var dialogresult = dialog.ShowDialog();
if (dialogresult != true)
{
return;
}
firstFrameTime = null;
writer = new VideoFileWriter();
writer.Open(dialog.FileName, (int)Math.Round(Image.Width, 0), (int)Math.Round(Image.Height, 0));
recording = true;
}
private void GetVideoDevices()
{
var devices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo device in devices)
VideoDevices.Add(device);
if (VideoDevices.Any())
CurrentDevice = VideoDevices[0];
else
System.Windows.MessageBox.Show("No webcam found");
}
private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
try
{
if (recording)
{
using (var bitmap = (Bitmap)eventArgs.Frame.Clone())
{
if (firstFrameTime != null)
writer.WriteVideoFrame(bitmap, DateTime.Now - firstFrameTime.Value);
else
{
writer.WriteVideoFrame(bitmap);
firstFrameTime = DateTime.Now;
}
}
}
using (var bitmap = (Bitmap)eventArgs.Frame.Clone())
{
var bi = new BitmapImage();
bi.BeginInit();
var ms = new MemoryStream();
bitmap.Save(ms, ImageFormat.Bmp);
ms.Seek(0, SeekOrigin.Begin);
bi.StreamSource = ms;
bi.EndInit();
//var bi = bitmap.ToBitmapImage();
bi.Freeze();
Dispatcher.CurrentDispatcher.Invoke(() => Image = bi);
}
}
catch (Exception exc)
{
System.Windows.MessageBox.Show("Error on:\n" + exc.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
StopCamera();
}
}
private void StopCamera()
{
if (videoSource != null && videoSource.IsRunning)
{
videoSource.SignalToStop();
videoSource.NewFrame -= video_NewFrame;
}
Image = null;
}
private RelayCommand startVideoCommand;
public RelayCommand StartVideoCommand
{
get => startVideoCommand ?? (startVideoCommand = new RelayCommand(
() =>
{
if (IsDesktopSource)
{
var rectangle = new Rectangle();
foreach (var screen in Screen.AllScreens)
{
rectangle = Rectangle.Union(rectangle, screen.Bounds);
}
videoSource = new ScreenCaptureStream(rectangle);
videoSource.NewFrame += video_NewFrame;
videoSource.Start();
}
else if (IsWebcamSource)
{
if (CurrentDevice != null)
{
videoSource = new VideoCaptureDevice(CurrentDevice.MonikerString);
videoSource.NewFrame += video_NewFrame;
videoSource.Start();
}
else
System.Windows.MessageBox.Show("Current device can't be null");
}
}
));
}
private RelayCommand saveSnapshotCommand;
public RelayCommand SaveSnapshotCommand
{
get => saveSnapshotCommand ?? (saveSnapshotCommand = new RelayCommand(
() =>
{
SaveSnapshot();
}
));
}
private RelayCommand startRecordingCommand;
public RelayCommand StartRecordingCommand
{
get => startRecordingCommand ?? (startRecordingCommand = new RelayCommand(
() =>
{
var dialog = new Microsoft.Win32.SaveFileDialog();
dialog.FileName = "Video1";
dialog.DefaultExt = ".avi";
dialog.AddExtension = true;
var dialogresult = dialog.ShowDialog();
if (dialogresult != true)
{
return;
}
firstFrameTime = null;
writer = new VideoFileWriter();
writer.Open(dialog.FileName, (int)Math.Round(Image.Width, 0), (int)Math.Round(Image.Height, 0));
recording = true;
}
));
}
private RelayCommand stopRecordingCommand;
public RelayCommand StopRecordingCommand
{
get => stopRecordingCommand ?? (stopRecordingCommand = new RelayCommand(
() =>
{
recording = false;
writer.Close();
writer.Dispose();
}
));
}
}
}