The security around accessing local devices is very tight. Starting the capture must be preceded by a user action.
Instead of starting the capture from the loaded event, you'll have to move it to a Click event.
Code behind:
public void StartCam()
{
VideoCaptureDevice dev = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();
if(CaptureDeviceConfiguration.RequestDeviceAccess() &&
CaptureDeviceConfiguration.AllowedDeviceAccess)
{
CaptureSource capture = new CaptureSource();
capture.VideoCaptureDevice = dev;
VideoBrush videoBrush = new VideoBrush();
videoBrush.SetSource(capture);
capture.Start();
WebCamRectangle.Fill = videoBrush;
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
StartCam();
}
Xaml:
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="49*" />
<RowDefinition Height="251*" />
</Grid.RowDefinitions>
<Rectangle Name="WebCamRectangle"
Stroke="Black" StrokeThickness="1" Grid.Row="1" />
<Button Content="Start" Height="25" HorizontalAlignment="Left"
Margin="12,12,0,0" Name="button1" VerticalAlignment="Top"
Width="135" Click="button1_Click" />
</Grid>