Tuesday, December 01, 2009

Five Minute Silverlight 4 Aides-Memoire #3 – WebCam/Microphone Device Support

<UserControl.Resources>
<
Style x:Key="ListBoxStyle" TargetType="ListBox">
<
Setter Property="ItemTemplate">
<
Setter.Value>
<
DataTemplate>
<
Image Margin="5" Source="{Binding}" Stretch="UniformToFill" Height="80" VerticalAlignment="Center"/>
</
DataTemplate>
</
Setter.Value>
</
Setter>
<
Setter Property="ItemsPanel">
<
Setter.Value>
<
ItemsPanelTemplate>
<
StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</
ItemsPanelTemplate>
</
Setter.Value>
</
Setter>
</
Style>
</
UserControl.Resources>



<StackPanel Grid.Row="2" Grid.Column="0">
<
Button Content="Start WebCam" Click="StartWebCam_Click" Height="30" />
<
Button Content="Snapshot" Click="Snapshot_Click" Height="30" />
</
StackPanel>
<
Rectangle Grid.Row="0" Stretch="Fill" Grid.Column="1" x:Name="CapturedImage" />
<ListBox Grid.Row="1" Margin="10" Grid.RowSpan="2" Grid.Column="1" x:Name="Snapshots" Style="{StaticResource ListBoxStyle}"/>


private void StartWebCam_Click(object sender, RoutedEventArgs e)
{
if (!CaptureDeviceConfiguration.AllowedDeviceAccess & !CaptureDeviceConfiguration.RequestDeviceAccess())
return;

VideoBrush videoBrush = new VideoBrush();
videoBrush.SetSource(captureSource);
CapturedImage.Fill = videoBrush;
this.Snapshots.ItemsSource = snaps;
captureSource.Start();
}


private void Snapshot_Click(object sender, RoutedEventArgs e)
{
captureSource.AsyncCaptureImage((image) => { snaps.Add(image); });
}

No comments: