@@ -158,3 +158,96 @@ private async void BackgroundBlur_Click(object sender, RoutedEventArgs e)
158
158
}
159
159
}
160
160
```
161
+
162
+ ### Background replacement
163
+
164
+ Background Replacement is a Video Effect that allows a person's background to be replacementred. In order to use Background Video Effect, you need to obtain a ` VideoEffectsLocalVideoStreamFeature ` feature from a valid ` LocalVideoStream ` .
165
+
166
+ To enable Background Replacement Video Effect:
167
+
168
+ - Add the ` BackgroundReplacementEffect ` instance to the MainPage.
169
+
170
+ ``` C#
171
+ public sealed partial class MainPage : Page
172
+ {
173
+ private BackgroundReplacementEffect backgroundReplacementVideoEffect = new BackgroundReplacementEffect ();
174
+ }
175
+ ```
176
+
177
+ - Create a method that obtains the ` VideoEFfects ` Feature subscribes to the events:
178
+
179
+ ``` C#
180
+ private async void LocalVideoEffectsFeature_VideoEffectEnabled (object sender , VideoEffectEnabledEventArgs e )
181
+ {
182
+ await Dispatcher .RunAsync (Windows .UI .Core .CoreDispatcherPriority .Normal , async () =>
183
+ {
184
+ BackgroundReplacement .IsChecked = true ;
185
+ });
186
+ }
187
+
188
+ private async void LocalVideoEffectsFeature_VideoEffectDisabled (object sender , VideoEffectDisabledEventArgs e )
189
+ {
190
+ await Dispatcher .RunAsync (Windows .UI .Core .CoreDispatcherPriority .Normal , async () =>
191
+ {
192
+ BackgroundReplacement .IsChecked = false ;
193
+ });
194
+ }
195
+
196
+ private void LocalVideoEffectsFeature_VideoEffectError (object sender , VideoEffectErrorEventArgs e )
197
+ {
198
+ String effectName = args .VideoEffectName ;
199
+ String errorCode = args .Code ;
200
+ String errorMessage = args .Message ;
201
+
202
+ Trace .WriteLine (" VideoEffects VideoEffectError on effect " + effectName + " with code "
203
+ + errorCode + " and error message " + errorMessage );
204
+ }
205
+ ```
206
+
207
+ - Set a custom background by passing in the image through a buffer.
208
+
209
+ ``` C#
210
+ // example of getting the image from storage folder
211
+ MemoryBuffer memoryBuffer = new MemoryBuffer (0 );
212
+ StorageFolder InstallationFolder = Windows .ApplicationModel .Package .Current .InstalledLocation ;
213
+ StorageFile file = InstallationFolder .GetFileAsync (" image.jpg" ).GetAwaiter ().GetResult ();
214
+ if (File .Exists (file .Path ))
215
+ {
216
+ byte [] imageBytes = File .ReadAllBytes (file .Path );
217
+ memoryBuffer = new MemoryBuffer ((uint )imageBytes .Length );
218
+ using (IMemoryBufferReference reference = memoryBuffer .CreateReference ())
219
+ {
220
+ byte * dataInBytes ;
221
+ uint capacityInBytes ;
222
+
223
+ (reference .As <IMemoryBufferByteAccess >()).GetBuffer (out dataInBytes , out capacityInBytes );
224
+ for (int i = 0 ; i < imageBytes .Length ; i ++ )
225
+ {
226
+ dataInBytes [i ] = imageBytes [i ];
227
+ }
228
+ }
229
+ return memoryBuffer ;
230
+ }
231
+ backgroundReplacementVideoEffect .Buffer = memoryBuffer ;
232
+ ```
233
+
234
+
235
+ - Enable and disable the Background Replacement effect:
236
+
237
+ ``` C#
238
+ private async void BackgroundReplacement_Click (object sender , RoutedEventArgs e )
239
+ {
240
+ if (localVideoEffectsFeature .IsEffectSupported (backgroundReplacementVideoEffect ))
241
+ {
242
+ var backgroundReplacementCheckbox = sender as CheckBox ;
243
+ if (backgroundReplacementCheckbox .IsChecked .Value )
244
+ {
245
+ localVideoEffectsFeature .EnableEffect (backgroundReplacementVideoEffect );
246
+ }
247
+ else
248
+ {
249
+ localVideoEffectsFeature .DisableEffect (backgroundReplacementVideoEffect );
250
+ }
251
+ }
252
+ }
253
+ ```
0 commit comments