【问题标题】:Aforge.NET Webcam Control XAML pageAforge.NET 网络摄像头控制 XAML 页面
【发布时间】:2013-12-06 10:04:08
【问题描述】:

我想在 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 页面中使用 Aforge.net 网络摄像头还有其他解决方案吗?

【问题讨论】:

  • 您查看过他们的文档吗?他们的网站?您是否尝试使用任何 WPF 图像控件或只是复制代码?
  • 我尝试使用图像,结果相同:没有。我检查了他们的文档,但没有找到有用的东西。
  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。

标签: c# .net wpf webcam aforge


【解决方案1】:

我成功地改变了 NewFrame 方法,如下所示:

   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*/
        }));     
    }

干杯!

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多