- Notifications
You must be signed in to change notification settings - Fork 961
/
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
usingSystem;
usingWindows.Devices.Enumeration;
usingWindows.Media.Capture;
usingWindows.Media.Effects;
usingWindows.Media.MediaProperties;
usingWindows.Storage.Streams;
usingWindows.UI;
usingWindows.UI.Xaml;
usingWindows.UI.Xaml.Controls;
usingWindows.UI.Xaml.Media;
usingWindows.UI.Xaml.Media.Imaging;
usingWindows.UI.Xaml.Navigation;
namespaceMediaCapture
{
publicsealedpartialclassMainPage:Page
{
// Captures photos from the webcam.
privateWindows.Media.Capture.MediaCapturemediaCapture;
// Used to display status messages.
privateBrushstatusBrush=newSolidColorBrush(Colors.Green);
// Used to display error messages.
privateBrushexceptionBrush=newSolidColorBrush(Colors.Red);
publicMainPage()
{
this.InitializeComponent();
}
// Shows a status message.
privatevoidShowStatusMessage(stringtext)
{
StatusBlock.Foreground=statusBrush;
StatusBlock.Text=text;
}
// Shows an error message.
privatevoidShowExceptionMessage(Exceptionex)
{
StatusBlock.Foreground=exceptionBrush;
StatusBlock.Text=ex.Message;
}
// Click event handler for the "Start Device" button.
privateasyncvoidStartDevice_Click(objectsender,RoutedEventArgse)
{
try
{
StartDevice.IsEnabled=false;
// Enumerate webcams.
ShowStatusMessage("Enumerating webcams...");
vardevInfoCollection=awaitDeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
if(devInfoCollection.Count==0)
{
ShowStatusMessage("No webcams found");
return;
}
// Initialize the MediaCapture object, choosing the first found webcam.
mediaCapture=newWindows.Media.Capture.MediaCapture();
varsettings=newWindows.Media.Capture.MediaCaptureInitializationSettings();
settings.VideoDeviceId=devInfoCollection[0].Id;
awaitmediaCapture.InitializeAsync(settings);
// We can now take photos and enable the grayscale effect.
TakePhoto.IsEnabled=true;
AddRemoveEffect.IsEnabled=true;
ShowStatusMessage("Device initialized successfully");
}
catch(Exceptionex)
{
ShowExceptionMessage(ex);
}
}
// Takes a photo from the webcam and displays it.
privateasyncvoidTakePhoto_Click(objectsender,RoutedEventArgse)
{
try
{
ShowStatusMessage("Taking photo...");
TakePhoto.IsEnabled=false;
// Capture the photo to an in-memory stream.
varphotoStream=newInMemoryRandomAccessStream();
awaitmediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(),photoStream);
ShowStatusMessage("Create photo file successful");
// Display the photo.
varbmpimg=newBitmapImage();
photoStream.Seek(0);
awaitbmpimg.SetSourceAsync(photoStream);
CapturedImage.Source=bmpimg;
TakePhoto.IsEnabled=true;
ShowStatusMessage("Photo taken");
}
catch(Exceptionex)
{
ShowExceptionMessage(ex);
TakePhoto.IsEnabled=true;
}
}
// Enables the grayscale effect.
privateasyncvoidAddRemoveEffect_Checked(objectsender,RoutedEventArgse)
{
try
{
AddRemoveEffect.IsEnabled=false;
VideoEffectDefinitiondef=newVideoEffectDefinition("GrayscaleTransform.GrayscaleEffect");
awaitmediaCapture.AddVideoEffectAsync(def,MediaStreamType.Photo);
ShowStatusMessage("Add effect to video preview successful");
AddRemoveEffect.IsEnabled=true;
}
catch(Exceptionex)
{
ShowExceptionMessage(ex);
}
}
// Removes the grayscale effect.
privateasyncvoidAddRemoveEffect_Unchecked(objectsender,RoutedEventArgse)
{
try
{
AddRemoveEffect.IsEnabled=false;
awaitmediaCapture.ClearEffectsAsync(Windows.Media.Capture.MediaStreamType.Photo);
ShowStatusMessage("Remove effect from preview successful");
AddRemoveEffect.IsEnabled=true;
}
catch(Exceptionex)
{
ShowExceptionMessage(ex);
}
}
}
}