0

I want to use a webcam in c# code with Aforge.NET, but It didin't work because I don't use forms.

See my code below :

    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;
                }
        }


    }
}

I tried this code in a form application, and it's work, but with a page, PictureBox are not referenced. Even if I reference it and create one in the code, this didn't work.

Can I, and how, use a PictureBox in a XAML Page ? Or there is an other solution tu use Aforge.net Webcam in Xaml Page ?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user2964381
  • 111
  • 1
  • 1
  • 9
  • Did you check their documentation? Their site? Did you try to use any WPF image control or just copied the code? – Panagiotis Kanavos Dec 06 '13 at 10:06
  • I tried with an Image, same result : Nothing. I checked their doc, but i didn't find something useful for this. – user2964381 Dec 06 '13 at 10:09
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Dec 06 '13 at 10:59

2 Answers2

2

I succeded with changing the NewFrame methode like this:

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

cheers !

user2964381
  • 111
  • 1
  • 1
  • 9
1

You could use WindowsFormsHost to integrate this Page in your WPF application.

If you need a WPF bitmap you may "convert" it like desribed here: Load a WPF BitmapImage from a System.Drawing.Bitmap

Community
  • 1
  • 1
JeffRSon
  • 10,404
  • 4
  • 26
  • 51