Aforge.NET网络摄像头控制XAML页面

时间:2013-12-06 10:04:09

标签: c# .net wpf webcam aforge

我想在Aforge.NET的c#代码中使用网络摄像头,但它没有用,因为我不使用表格。

请参阅下面的代码:

    public partial class CapturePage : Page
    {
        private bool DeviceExist = false;
        private FilterInfoCollection videoDevices;
        private VideoCaptureDevice videoSource = null;

        public CapturePage()
        {
            InitializeComponent();
          getCamList();
          startVideo();
        }

        // get the devices name
        private void getCamList()
        {
            try
            {
                videoDevices = new  FilterInfoCollection(FilterCategory.VideoInputDevice);
                if (videoDevices.Count == 0)
                    throw new ApplicationException();

                DeviceExist = true;

            }
            catch (ApplicationException)
            {
                DeviceExist = false;
            }
        }



               //toggle start and stop button
               private void startVideo() // as it's say
               {
                if (DeviceExist)
                {
                    videoSource = new VideoCaptureDevice(videoDevices[0].MonikerString); // the only one webcam
                    videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
                    CloseVideoSource();
                  //  videoSource.DesiredFrameSize = new Size(160, 120); // deprecated ?
                    //videoSource.DesiredFrameRate = 10;
                    videoSource.Start();

                }
                else
                {
                    // error
                }
            }
            else
            {
                if (videoSource.IsRunning)
                {
                    timer1.Enabled = false;
                    CloseVideoSource();

                }
            }
        }

        //eventhandler if new frame is ready
        private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            Bitmap img = (Bitmap)eventArgs.Frame.Clone();
            //do processing here
            pictureBox1.Image = img; // But I can't have pictureBox in xaml ??
        }

        //close the device safely
        private void CloseVideoSource()
        {
            if (!(videoSource == null))
                if (videoSource.IsRunning)
                {
                    videoSource.SignalToStop();
                    videoSource = null;
                }
        }


    }
}

我在表单应用程序中尝试了这段代码,但是它有效,但是对于页面,不会引用PictureBox。即使我引用它并在代码中创建一个,这也不起作用。

我可以以及如何在XAML页面中使用PictureBox吗?或者在Xaml Page中有另一个使用Aforge.net网络摄像头的解决方案?

2 个答案:

答案 0 :(得分:2)

我成功地改变了这样的New Frame方法:

   private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        System.Drawing.Image imgforms = (System.Drawing.Bitmap)eventArgs.Frame.Clone();

        BitmapImage bi = new BitmapImage();
        bi.BeginInit();

        MemoryStream ms = new MemoryStream();
        imgforms.Save(ms, ImageFormat.Bmp);
        ms.Seek(0, SeekOrigin.Begin);

        bi.StreamSource = ms;
        bi.EndInit();

        //Using the freeze function to avoid cross thread operations 
        bi.Freeze();

        //Calling the UI thread using the Dispatcher to update the 'Image' WPF control         
        Dispatcher.BeginInvoke(new ThreadStart(delegate
        {
            ImageWebcam.Source = bi; /*frameholder is the name of the 'Image' WPF control*/
        }));     
    }
欢呼!

答案 1 :(得分:1)

您可以使用WindowsFormsHost将此Page集成到WPF应用程序中。

如果你需要一个WPF位图,你可以像在这里所描述的那样“转换”它:Load a WPF BitmapImage from a System.Drawing.Bitmap