-
Notifications
You must be signed in to change notification settings - Fork 962
/
Copy pathwalkthrough-creating-a-windows-store-app-using-wrl-and-media-foundation_8.cs
141 lines (125 loc) · 4.83 KB
/
walkthrough-creating-a-windows-store-app-using-wrl-and-media-foundation_8.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
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
using System;
using Windows.Devices.Enumeration;
using Windows.Media.Capture;
using Windows.Media.Effects;
using Windows.Media.MediaProperties;
using Windows.Storage.Streams;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
namespace MediaCapture
{
public sealed partial class MainPage : Page
{
// Captures photos from the webcam.
private Windows.Media.Capture.MediaCapture mediaCapture;
// Used to display status messages.
private Brush statusBrush = new SolidColorBrush(Colors.Green);
// Used to display error messages.
private Brush exceptionBrush = new SolidColorBrush(Colors.Red);
public MainPage()
{
this.InitializeComponent();
}
// Shows a status message.
private void ShowStatusMessage(string text)
{
StatusBlock.Foreground = statusBrush;
StatusBlock.Text = text;
}
// Shows an error message.
private void ShowExceptionMessage(Exception ex)
{
StatusBlock.Foreground = exceptionBrush;
StatusBlock.Text = ex.Message;
}
// Click event handler for the "Start Device" button.
private async void StartDevice_Click(object sender, RoutedEventArgs e)
{
try
{
StartDevice.IsEnabled = false;
// Enumerate webcams.
ShowStatusMessage("Enumerating webcams...");
var devInfoCollection = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
if (devInfoCollection.Count == 0)
{
ShowStatusMessage("No webcams found");
return;
}
// Initialize the MediaCapture object, choosing the first found webcam.
mediaCapture = new Windows.Media.Capture.MediaCapture();
var settings = new Windows.Media.Capture.MediaCaptureInitializationSettings();
settings.VideoDeviceId = devInfoCollection[0].Id;
await mediaCapture.InitializeAsync(settings);
// We can now take photos and enable the grayscale effect.
TakePhoto.IsEnabled = true;
AddRemoveEffect.IsEnabled = true;
ShowStatusMessage("Device initialized successfully");
}
catch (Exception ex)
{
ShowExceptionMessage(ex);
}
}
// Takes a photo from the webcam and displays it.
private async void TakePhoto_Click(object sender, RoutedEventArgs e)
{
try
{
ShowStatusMessage("Taking photo...");
TakePhoto.IsEnabled = false;
// Capture the photo to an in-memory stream.
var photoStream = new InMemoryRandomAccessStream();
await mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), photoStream);
ShowStatusMessage("Create photo file successful");
// Display the photo.
var bmpimg = new BitmapImage();
photoStream.Seek(0);
await bmpimg.SetSourceAsync(photoStream);
CapturedImage.Source = bmpimg;
TakePhoto.IsEnabled = true;
ShowStatusMessage("Photo taken");
}
catch (Exception ex)
{
ShowExceptionMessage(ex);
TakePhoto.IsEnabled = true;
}
}
// Enables the grayscale effect.
private async void AddRemoveEffect_Checked(object sender, RoutedEventArgs e)
{
try
{
AddRemoveEffect.IsEnabled = false;
VideoEffectDefinition def = new VideoEffectDefinition("GrayscaleTransform.GrayscaleEffect");
await mediaCapture.AddVideoEffectAsync(def, MediaStreamType.Photo);
ShowStatusMessage("Add effect to video preview successful");
AddRemoveEffect.IsEnabled = true;
}
catch (Exception ex)
{
ShowExceptionMessage(ex);
}
}
// Removes the grayscale effect.
private async void AddRemoveEffect_Unchecked(object sender, RoutedEventArgs e)
{
try
{
AddRemoveEffect.IsEnabled = false;
await mediaCapture.ClearEffectsAsync(Windows.Media.Capture.MediaStreamType.Photo);
ShowStatusMessage("Remove effect from preview successful");
AddRemoveEffect.IsEnabled = true;
}
catch (Exception ex)
{
ShowExceptionMessage(ex);
}
}
}
}