MainPage.xaml.cs
上传用户:haqqyyuan
上传日期:2021-05-06
资源大小:26k
文件大小:3k
源码类别:

SilverLight

开发平台:

C#

  1. 
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Media;
  5. namespace SL4Demo
  6. {
  7.     public partial class MainPage : UserControl
  8.     {
  9.         // initialize the new instance of the Capture Source
  10.         private CaptureSource captureSource = new CaptureSource();
  11.         public MainPage()
  12.         {
  13.             InitializeComponent();
  14.             this.Loaded += new RoutedEventHandler(MainPage_Loaded);
  15.             btnPlayCapture.Click += new RoutedEventHandler(btnPlayCapture_Click);
  16.             btnStopCapture.Click += new RoutedEventHandler(btnStopCapture_Click);
  17.             btnCaptureDevice.Click += new RoutedEventHandler(btnCaptureDevice_Click);
  18.         }
  19.         void btnCaptureDevice_Click(object sender, RoutedEventArgs e)
  20.         {
  21.             TryCaptureDevice();
  22.         }
  23.         void btnStopCapture_Click(object sender, RoutedEventArgs e)
  24.         {
  25.             // Stop capturing
  26.             captureSource.Stop();
  27.             btnPlayCapture.IsEnabled = true;
  28.             btnStopCapture.IsEnabled = false;
  29.         }
  30.         void btnPlayCapture_Click(object sender, RoutedEventArgs e)
  31.         {
  32.             // If the device is already capturing Stop it
  33.             if (captureSource.State == CaptureState.Started)
  34.             {
  35.                 captureSource.Stop();
  36.             }
  37.             // Start capturing
  38.             captureSource.Start();
  39.             btnPlayCapture.IsEnabled = false;
  40.             btnStopCapture.IsEnabled = true;
  41.         }
  42.         void MainPage_Loaded(object sender, RoutedEventArgs e)
  43.         {
  44.             TryCaptureDevice();
  45.         }
  46.         private void TryCaptureDevice()
  47.         {
  48.             // Get the default video capture device
  49.             VideoCaptureDevice videoCaptureDevice = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();
  50.             if (videoCaptureDevice == null)
  51.             {
  52.                 // Default video capture device is not setup
  53.                 btnPlayCapture.IsEnabled = false;
  54.                 btnStopCapture.IsEnabled = false;
  55.                 btnCaptureDevice.IsEnabled = true;
  56.                 MessageBox.Show("You don't have any default capture device");
  57.             }
  58.             else
  59.             {
  60.                 btnPlayCapture.IsEnabled = false;
  61.                 btnStopCapture.IsEnabled = false;
  62.                 // Set the Capture Source to the VideoBrush of the rectangle
  63.                 VideoBrush videoBrush = new VideoBrush();
  64.                 videoBrush.SetSource(captureSource);
  65.                 rectWebCamView.Fill = videoBrush;
  66.                 // Check if the Silverlight has already access to the device or grant access from the user
  67.                 if (CaptureDeviceConfiguration.AllowedDeviceAccess || CaptureDeviceConfiguration.RequestDeviceAccess())
  68.                 {
  69.                     btnPlayCapture.IsEnabled = true;
  70.                     btnStopCapture.IsEnabled = false;
  71.                     btnCaptureDevice.IsEnabled = false;
  72.                 }
  73.             }
  74.         }
  75.     }
  76. }