<usercontrol x:class="WebCamera.MainPage" xmlns:x="#unknown"> xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <grid x:name="LayoutRoot" removed="White"> <grid.rowdefinitions> <rowdefinition height="32" /> <rowdefinition /> </grid.rowdefinitions> <grid.columndefinitions> <columndefinition /> <columndefinition /> </grid.columndefinitions> <Button x:Name="startVideo" Grid.Row="0" Grid.Column="0" Content="Start Camera!" Click="startVideo_Click" /> <Button x:Name="snap" Grid.Row="0" Grid.Column="1" Content="Take Picture!" Click="snap_Click" IsEnabled="False" /> <Image x:Name="snapImage" Grid.Row="1" Grid.Column="1" /> <rectangle x:name="video" grid.row="1" grid.column="0" /> </grid> </usercontrol>
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.IO; namespace WebCamera { public partial class MainPage : UserControl { private CaptureSource cs = null; public MainPage() { InitializeComponent(); } private void startVideo_Click(object sender, RoutedEventArgs e) { bool ok = CaptureDeviceConfiguration.AllowedDeviceAccess; if (!ok) { ok = CaptureDeviceConfiguration.RequestDeviceAccess(); } if (ok) { cs = new CaptureSource() { VideoCaptureDevice = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice() }; VideoBrush vidBrush = new VideoBrush(); vidBrush.SetSource(cs); video.Fill = vidBrush; cs.Start(); snap.IsEnabled = true; } } private void snap_Click(object sender, RoutedEventArgs e) { cs.AsyncCaptureImage((bitmap) = > { Dispatcher.BeginInvoke(() => { snapImage.Source = bitmap; }); }); } }}
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)