Real-time frame of webcam capture transfer between forms in winforms

Ezgi Ecevit 106 Reputation points
2021-09-01T13:58:47.603+00:00

I am designing an UI using windows forms app and i need to transfer the real-time frame of webcam capture between the forms.
128334-form2.png

This is my form2 and the webcam will capture the frame here and its code is shown below.

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
using System.Windows.Forms;  

using AForge.Video;  
using AForge.Video.DirectShow;  


namespace MUI  
{  
    public partial class Form2 : Form  
    {  

        public Form2()  
        {  
            InitializeComponent();  
        }  

        FilterInfoCollection filterInfoCollection;  
        VideoCaptureDevice videoCaptureDevice;  





        public void videoCaptureDevice_NewFrame(object sender, NewFrameEventArgs eventArgs)  
        {  
            pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();  
        }  

        public Image img;  
        private void button1_Click(object sender, EventArgs e)  
        {  
            try  
            {  
                var cam = filterInfoCollection[comboBox1.SelectedIndex].MonikerString;  
                videoCaptureDevice = new VideoCaptureDevice(cam);  
                videoCaptureDevice.NewFrame += videoCaptureDevice_NewFrame;  
                videoCaptureDevice.Start();  
                img = pictureBox1.Image;  
                Form7 f7 = new Form7();  
                f7.Show();  
            }  
            catch (Exception)  
            {  

                throw;  
            }  


        }  

        private void button3_Click(object sender, EventArgs e)  
        {  

            videoCaptureDevice.Stop();  
            pictureBox1.Image = null;  
            this.Close();  
        }  

        private void button2_Click(object sender, EventArgs e)  
        {  
            videoCaptureDevice.Stop();  
            pictureBox1.Image = null;  
        }  

        public void Form2_Load(object sender, EventArgs e)  
        {  
            try  
            {  
                #region Devices  
                filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);  
                foreach (FilterInfo filterInfo in filterInfoCollection)  
                {  
                    comboBox1.Items.Add(filterInfo.Name);  
                }  
                comboBox1.SelectedIndex = 0;  
                videoCaptureDevice = new VideoCaptureDevice();  
                #endregion  

            }  
            catch (Exception)  
            {  

                throw;  
            }  
        }  

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)  
        {  
            if (videoCaptureDevice.IsRunning == true)  
                videoCaptureDevice.Stop();  
        }  


    }  
}  

128279-form3.png
And here's the form3 that i want to transfer the frame.

I haven't write anything yet in the form2 because I couldn't send the frame.
How can I achieve that?

Developer technologies | Windows Forms
Developer technologies | C#
{count} vote

Accepted answer
  1. Ezgi Ecevit 106 Reputation points
    2021-09-02T08:26:28.913+00:00

    Form2 Code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    using AForge.Video;
    using AForge.Video.DirectShow;
    
    
    namespace MUI
    {
        public partial class Form2 : Form
        {
    
    
            public Form2()
            {
                InitializeComponent();
            }
    
            FilterInfoCollection filterInfoCollection;
            VideoCaptureDevice videoCaptureDevice;
    
    
    
    
    
            public void videoCaptureDevice_NewFrame(object sender, NewFrameEventArgs eventArgs)
            {
                Form3 f3 = new Form3();
                pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
                f3.pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Form3 f3 = new Form3();
                videoCaptureDevice = new VideoCaptureDevice(filterInfoCollection[comboBox1.SelectedIndex].MonikerString);
                videoCaptureDevice.NewFrame += videoCaptureDevice_NewFrame;
                videoCaptureDevice.NewFrame += f3.videoCaptureDevice_NewFrame;
                videoCaptureDevice.Start();
    
                f3.Show();
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
    
                videoCaptureDevice.Stop();
                pictureBox1.Image = null;
                this.Close();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                videoCaptureDevice.Stop();
                pictureBox1.Image = null;
            }
    
            public void Form2_Load(object sender, EventArgs e)
            {
    
                filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                foreach (FilterInfo filterInfo in filterInfoCollection)
                {
                    comboBox1.Items.Add(filterInfo.Name);
                }
                comboBox1.SelectedIndex = 0;
                videoCaptureDevice = new VideoCaptureDevice();
    
            }
    
            private void Form2_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (videoCaptureDevice.IsRunning == true)
                    videoCaptureDevice.Stop();
            }
    
    
        }
    }
    

    Form3 Code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    using AForge.Video;
    
    namespace MUI
    {
    
        public partial class Form3 : Form
        {
    
            public Form3()
            {
                InitializeComponent();
            }
    
    
    
            private void Form3_Load(object sender, EventArgs e)
            {
            }
    
            public void videoCaptureDevice_NewFrame(object sender, NewFrameEventArgs eventArgs)
            {
                pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
            }
    
    
    
            private void button1_Click(object sender, EventArgs e)
            {
    
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                this.Close();
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                this.Close();
            }
        }
    }
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.