MainPage.xaml.cs
上传用户:haqqyyuan
上传日期:2021-05-06
资源大小:26k
文件大小:3k
-
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- namespace SL4Demo
- {
- public partial class MainPage : UserControl
- {
- // initialize the new instance of the Capture Source
- private CaptureSource captureSource = new CaptureSource();
- public MainPage()
- {
- InitializeComponent();
- this.Loaded += new RoutedEventHandler(MainPage_Loaded);
- btnPlayCapture.Click += new RoutedEventHandler(btnPlayCapture_Click);
- btnStopCapture.Click += new RoutedEventHandler(btnStopCapture_Click);
- btnCaptureDevice.Click += new RoutedEventHandler(btnCaptureDevice_Click);
- }
- void btnCaptureDevice_Click(object sender, RoutedEventArgs e)
- {
- TryCaptureDevice();
- }
- void btnStopCapture_Click(object sender, RoutedEventArgs e)
- {
- // Stop capturing
- captureSource.Stop();
- btnPlayCapture.IsEnabled = true;
- btnStopCapture.IsEnabled = false;
- }
- void btnPlayCapture_Click(object sender, RoutedEventArgs e)
- {
- // If the device is already capturing Stop it
- if (captureSource.State == CaptureState.Started)
- {
- captureSource.Stop();
- }
- // Start capturing
- captureSource.Start();
- btnPlayCapture.IsEnabled = false;
- btnStopCapture.IsEnabled = true;
- }
- void MainPage_Loaded(object sender, RoutedEventArgs e)
- {
- TryCaptureDevice();
- }
- private void TryCaptureDevice()
- {
- // Get the default video capture device
- VideoCaptureDevice videoCaptureDevice = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();
- if (videoCaptureDevice == null)
- {
- // Default video capture device is not setup
- btnPlayCapture.IsEnabled = false;
- btnStopCapture.IsEnabled = false;
- btnCaptureDevice.IsEnabled = true;
- MessageBox.Show("You don't have any default capture device");
- }
- else
- {
- btnPlayCapture.IsEnabled = false;
- btnStopCapture.IsEnabled = false;
- // Set the Capture Source to the VideoBrush of the rectangle
- VideoBrush videoBrush = new VideoBrush();
- videoBrush.SetSource(captureSource);
- rectWebCamView.Fill = videoBrush;
- // Check if the Silverlight has already access to the device or grant access from the user
- if (CaptureDeviceConfiguration.AllowedDeviceAccess || CaptureDeviceConfiguration.RequestDeviceAccess())
- {
- btnPlayCapture.IsEnabled = true;
- btnStopCapture.IsEnabled = false;
- btnCaptureDevice.IsEnabled = false;
- }
- }
- }
- }
- }