none
2 webcam different pictureBox RRS feed

  • Question

  • How to correctly address the video stream for simultaneous output in two PictureBox?
    A code has now been generated for displaying images from webcams on a form (window). Required with only one webcam

    get an image in one picturebox, but not in the other, then vice versa by manually switching the number

    videoDevices from 0 to 1 and vice versa ("0" is the number of the connected camera).

    videoSource = new VideoCaptureDevice (videoDevices [0] .MonikerString);

    videoSource2 = new VideoCaptureDevice (videoDevices [1] .MonikerString);

    When "0" on videoDevices of both sources, there is an image from the camera on one picturebox, but not on the other. How to specify a video stream,

    so that when "0" there was an image from the camera on both picturebox (it is known that the attachment to the void videoSource_NewFrame method

    this.pictureBox2.Image = (Image) eventArgs.Frame.Clone (); allows you to incorrectly solve this problem)

    When "0" on videoDevices of the same source and "1" on the videoDevices of the other source, there are already no pictures on both picturebox

    cameras. Why? How to correctly address the video stream so that there is an image in pictureBox1.

    I give the code:

    using System; //example_professorweb
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using AForge.Video;
    using AForge.Video.DirectShow;
    using System.Threading;

        public class Form1 : Form
        {
    private FilterInfoCollection videoDevices = null;

    private VideoCaptureDevice videoSource = null;
    private VideoCaptureDevice videoSource2 = null;
    Bitmap image1;
    Bitmap image2;

            public Form1()
            {
                InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    Thread.Sleep(10);
    videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

    videoSource = new VideoCaptureDevice(videoDevices[1].MonikerString);
    videoSource.NewFrame += new NewFrameEventHandler(videoSource_NewFrame);
    videoSource2 = new VideoCaptureDevice(videoDevices[0].MonikerString);
    videoSource2.NewFrame += new NewFrameEventHandler(videoSource2_NewFrame);
    Thread.Sleep(500);
    videoSource.Start();
    Thread.Sleep(500);
    videoSource2.Start();
    }

    void videoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
    this.pictureBox1.Image = (Image)eventArgs.Frame.Clone();
    }

    void videoSource2_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
    this.pictureBox2.Image = (Image)eventArgs.Frame.Clone();


    private void InitializeComponent()
            {
                this.pictureBox1 = new System.Windows.Forms.PictureBox();
    ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
    this.pictureBox2 = new System.Windows.Forms.PictureBox();
                ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
                this.SuspendLayout();
                // 
                // pictureBox1 pic
    this.pictureBox1.BackColor = System.Drawing.Color.Gainsboro;
    this.pictureBox1.Location = new System.Drawing.Point(100, 300);
                this.pictureBox1.Name = "pictureBox1";
                this.pictureBox1.Size = new System.Drawing.Size(294, 220);
                this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
                this.pictureBox1.TabIndex = 0;
                this.pictureBox1.TabStop = false;
    image1 = new Bitmap(@"d:\1.jpg");
    pictureBox1.Image = (Image)image1;
    // pictureBox2 pic2
    this.pictureBox2.BackColor = System.Drawing.Color.Gainsboro;
    this.pictureBox2.Location = new System.Drawing.Point(100, 600);
                this.pictureBox2.Name = "pictureBox2";
                this.pictureBox2.Size = new System.Drawing.Size(294, 220);
                this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
                this.pictureBox2.TabIndex = 0;
                this.pictureBox2.TabStop = false;
    image2 = new Bitmap(@"d:\2.jpg");
    pictureBox2.Image = (Image)image2;
            // 
                // Form1 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(600, 900);
                this.Controls.Add(this.pictureBox1);
    this.Controls.Add(this.pictureBox2);
                this.Name = "Form2";
                this.Text = "soft1";
                this.Load += new System.EventHandler(this.Form1_Load);
                ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
    ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
                this.ResumeLayout(false);

    this.firstLabel = new System.Windows.Forms.Label();
    this.secondLabel = new System.Windows.Forms.Label();
    this.labelsToolTip = new System.Windows.Forms.ToolTip(new System.ComponentModel.Container());
    this.SuspendLayout();
    // 
    // firstLabel label pic1
    this.firstLabel.AutoSize = true;
    this.firstLabel.BorderStyle = System.Windows.Forms.BorderStyle.None;
    this.firstLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.firstLabel.Location = new System.Drawing.Point(160, 280);
    this.firstLabel.Name = "firstLabel";
    this.firstLabel.Size = new System.Drawing.Size(92, 18);
    this.firstLabel.TabIndex = 0;
    this.firstLabel.Text = "pic1";
    this.labelsToolTip.SetToolTip(this.firstLabel, "First Label");
    // 
    // secondLabel label pic2)
    this.secondLabel.AutoSize = true;
    this.secondLabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    this.secondLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.secondLabel.Location = new System.Drawing.Point(12, 69);
    this.secondLabel.Name = "secondLabel";
    this.secondLabel.Size = new System.Drawing.Size(133, 18);
    this.secondLabel.TabIndex = 1;
    this.secondLabel.Tag = "";
    this.secondLabel.Text = "This is another Label.";
    this.labelsToolTip.SetToolTip(this.secondLabel, "Second Label");
    // 
    // ToolTipExampleForm http://www.java2s.com/Code/CSharp/GUI-Windows-Form/AddTooltipsforLabel.htm
    this.Controls.Add(this.secondLabel);
    this.Controls.Add(this.firstLabel);
    this.PerformLayout();

    }

    private System.Windows.Forms.PictureBox pictureBox1;
    private System.Windows.Forms.PictureBox pictureBox2;
    private System.Windows.Forms.Label firstLabel;
    private System.Windows.Forms.Label secondLabel;
    private System.Windows.Forms.ToolTip labelsToolTip;
        }

    static class Program
        {
            [STAThread]
            static void Main()
            { //http://csharp-me.blogspot.com/2014/02/make-simple-webcam-in-c.html
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());

            }
        }       

    • Moved by CoolDadTx Friday, November 9, 2018 3:10 PM Winforms related
    • Moved by Stanly Fan Monday, November 12, 2018 5:54 AM
    Friday, November 9, 2018 1:28 PM

All replies

  • Hi,

    According to your description, your issue is more related about AForge.Net. it will be more appropriate to ask your question at AForge.Net forum.

    Thanks for you understanding.

    Regards,

    Stanly

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 12, 2018 5:52 AM