question

EzgiEcevit-2777 avatar image
1 Vote"
EzgiEcevit-2777 asked EzgiEcevit-2777 published

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

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][1]


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][2]
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?
[1]: /answers/storage/attachments/128334-form2.png
[2]: /answers/storage/attachments/128279-form3.png

dotnet-csharpwindows-forms
form2.png (9.8 KiB)
form3.png (12.9 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@EzgiEcevit-2777, Since this problem is related to AForge, it is a 3rd party product. I recommend that you could ask the question in the Stack Overflow.


0 Votes 0 ·

I asked there but I couldn't get the exact answer. However, I solved the problem and I'll share it here so that people can see. Thanks for your concern.

0 Votes 0 ·

1 Answer

EzgiEcevit-2777 avatar image
1 Vote"
EzgiEcevit-2777 answered EzgiEcevit-2777 published

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


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.