Software how to write simple but working code
proposed by Prof.Satoshi Nishimura image00 / Jichi Med Univ(JAPAN)

Specs: Camera viewer always in front

E-mail: If interested (snishi-tky(a-mark)ninus.ocn.ne.jp ) image00

Back to top image00
Software DOWNLOAD (exe in ZIP)



		
            using OpenCvSharp;
            using System;
            using System.Drawing;
            using System.Drawing.Imaging;
            using System.Windows.Forms;
            using System.IO;
            
            
            //  added to WEBCAM //
            using System.Text;
            using AForge.Video;
            using AForge.Video.DirectShow;
            //  added to WEBCAM //
            
            
            namespace WindowsFormsApp2
            {
                public partial class Form1 : Form
                {
                    //camera moduels
                    public int maintimerfreq = 5;
                    public int prevfile_cam0 = 0, prevfile_cam1 = 0, prevfile_cam2 = 0, prevfile_cam3 = 0;
                    public int quitcamflag = 0;
                    public int cam0waitflag = 0;
                    public int cam1waitflag = 0;
                    public int cam2waitflag = 0;
                    public int cam3waitflag = 0;
                    public int cam0time = 0;
                    public int cam1time = 0;
                    public int cam2time = 0;
                    public int cam3time = 0;
            
                    //  WEBCAM //
                    private FilterInfoCollection videoDevices0;
                    private VideoCaptureDevice videoDevice0;
                    private VideoCapabilities[] videoCapabilities0;
                    private CameraControlFlags controlflag0;
                    Bitmap camimg0;
            
                    public int web0flag = 0, web0_Width = 0, web0_Height = 0;
            
                    //main timer
                    public int maintimer = 0;
                    public int beforetimetotal = 0;
                    public int beforetime = 0;
                    public int beforetime1 = 0;
                    public int beforetime2 = 0;
                    public int beforetime3 = 0;
                    public int beforetime0 = 0;
                    //keep mode
                    public int keepflag = 0;
            
            
                    //////////////////////////////////////////////
                    /////////MAIN    MAIN    MAIN  /////////
                    ///////////////////////////////////////////////
                    /// entry point
                    /// //////////////////////////////////////////
                    //INIT
                    public Form1()
                    {
                        InitializeComponent();
                    }
                    //LOAD
                    private void Form1_Load(object sender, EventArgs e)    // INITIALIZE  //
                    {
            
                        // webcam 0 load //
                        try
                        {
                            videoDevices0 = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                            if (videoDevices0.Count != 0)
                            {
                                comboBox8.Items.Clear();
                                foreach (FilterInfo device in videoDevices0)
                                {
                                    comboBox8.Items.Add(device.Name);
                                }
                                comboBox8.SelectedIndex = videoDevices0.Count - 1;
                                comboBox9.Enabled = true;
                            }
                            else
                            {
                                comboBox8.Items.Clear();
                                comboBox8.Items.Add("no devices");
                                comboBox8.SelectedIndex = 0;
                            }
                        }
            
                        catch (Exception error)
                        {
                            button1.Text = error.ToString();
                        }
                    }
            
                    //FORM CLOSING button
                    private void button16_Click(object sender, EventArgs e)
                    {
                        timer2.Enabled = false;
            
                        Application.Exit();
                    }
            
                    //FORM close
                    private void Form1_FormClosing(object sender, FormClosingEventArgs e)   //QUIT & close Cameras
                    {
                        ////WEBcam close 
                        //
                        if (web0flag > 0)
                        {
                            if (videoDevice0.IsRunning)
                            {
                                videoDevice0.SignalToStop();
                                videoDevice0.WaitForStop();
                                videoDevice0 = null;
                            }
                        }
            
            
                    }
            
                    /////////MAIN   TIMER1  /////////
                    /////////MAIN   TIMER1  /////////
                    /////////MAIN   TIMER1  /////////
                    ///
                    //MAIN timer interval
                    private void numericUpDown5_ValueChanged(object sender, EventArgs e)
                    {
                        // timer1.Interval = (int)numericUpDown5.Value;
                    }
            
            
                    //operation panel ON
                    private void label11_Click(object sender, EventArgs e)
                    {
                        cam0disp();
                    }
                    private void cam0disp()
                    {
                        bool check = true;
                        if (button1.Visible == true)
                        {
                            check = false;
                        }
                        else
                        {
                            check = true;
                        }
                        button2.Visible = check;
                        button1.Visible = check;
                        button48.Visible = check;
                        button77.Visible = check;
                        numericUpDown1.Visible = check;
                        comboBox8.Visible = check;
                        comboBox9.Visible = check;
                        label59.Visible = check;
                        label96.Visible = check;
                    }
                    void videoDevice_NewFrame0(object sender, NewFrameEventArgs eventArgs)
                    {
                        DateTime dt2 = DateTime.Now;
                        cam0time = (((dt2.Hour * 60 + dt2.Minute) * 60) + dt2.Second) * 1000 + dt2.Millisecond;
                        videoDevice0.NewFrame -= new NewFrameEventHandler(videoDevice_NewFrame0);
                        cam0waitflag = 1;
                        if (InvokeRequired)
                        {
                            Invoke(new Action(videoDevice_NewFrame0), sender, eventArgs);
                        }
                        else
                        {
                            try
                            {
                                camimg0 = (Bitmap)eventArgs.Frame.Clone();
                                pictureBox2.Image = camimg0;
                            }
                            catch (Exception ex)
                            { button1.Text = ex.ToString(); }
                        }
                        cam0waitflag = 0;
                        videoDevice0.NewFrame += new NewFrameEventHandler(videoDevice_NewFrame0);
                    }
            
                    private void pictureBox2_Click(object sender, EventArgs e)
                    {
                        pictureBox2.SendToBack();
                    }
            
                    private void button3_Click(object sender, EventArgs e)
                    {
                        web0flag = 0;
                        //videoDevice0.NewFrame -= new NewFrameEventHandler(videoDevice_NewFrame0);
                        if (videoDevice0.IsRunning)
                        {
                            videoDevice0.SignalToStop();
                            videoDevice0.WaitForStop();
                        }
                        label11.ForeColor = Color.Black;
                        button1.BackColor = Color.Gray;
                        comboBox9.Enabled = true;
                        Application.Exit();
                    }
            
            
            
                    ///WEBCAM start  
                    // Streaming camera START
                    private void button1_Click(object sender, EventArgs e)    // LOAD CAPTURE HDMI Camera  //
                    {
            
                        if (web0flag==1)
                        { 
                            
                         }
                        else
                        {
                            label1.ForeColor = Color.Red;
                            label1.BackColor = Color.Black;
                            label1.Visible = true;
                            timer2.Enabled = true;
                            timer2.Interval = 1000 *5;
            
            
                            button1.Enabled = true;
                            videoDevice0.NewFrame += new NewFrameEventHandler(videoDevice_NewFrame0);
                            //videoDevice0.DesiredFrameRate = 20;
                            videoDevice0.Start();
                            button1.BackColor = Color.Red;
                            label11.ForeColor = Color.Red;
                            web0flag = 1;
                            button77.Text = "dispose";
                            comboBox8.Enabled = false;
                            comboBox9.Enabled = false;
                        }
            
                       
            
                        pictureBox2.Visible = true;
                        pictureBox2.Size = new System.Drawing.Size(640,480);
                        pictureBox2.Width = 640;
                        pictureBox2.Height = 480;
                        pictureBox2.Left = 0;  // 1220 - web0_Width;
                        pictureBox2.Top = 0; // 460 - web0_Height;
                        pictureBox2.BringToFront();
                        this.Size = new System.Drawing.Size(640, 480);
                    }
            
                    ///WEBCAM Stop
                    // Streaming camera STOP
                    private void button41_Click(object sender, EventArgs e)
                    {
                        web0flag = 0;
                        videoDevice0.NewFrame -= new NewFrameEventHandler(videoDevice_NewFrame0);
                        if (videoDevice0.IsRunning)
                        {
                            videoDevice0.SignalToStop();
                              videoDevice0.WaitForStop();
                        }
                        label11.ForeColor = Color.Black;
                        button1.BackColor = Color.Gray;
                        comboBox9.Enabled = true;
                    }
            
                    /// WEBCAM property       
                    private void button48_Click_1(object sender, EventArgs e)
                    {
                        videoDevice0.DisplayPropertyPage(IntPtr.Zero);
                    }
            
                    private void timer2_Tick(object sender, EventArgs e)
                    {
                        if (label1.Visible == true)
                        {
                            label1.Visible = false;
                        }
                        else
                        {
                            label1.Visible = true;
                            label1.BringToFront();
                            this.TopMost = true;
                            this.Focus();
                            this.BringToFront();
                        }
                    }
            
                    ///WEBCAM resolution
                    private void comboBox9_SelectedIndexChanged(object sender, EventArgs e)
                    {
                        videoDevice0.VideoResolution = videoCapabilities0[comboBox9.SelectedIndex];
                        web0_Width = videoCapabilities0[comboBox9.SelectedIndex].FrameSize.Width;
                        web0_Height = videoCapabilities0[comboBox9.SelectedIndex].FrameSize.Height;
                    }
                    ///WEBCAM camera select
                    private void comboBox8_SelectedIndexChanged(object sender, EventArgs e)
                    {
                        comboBox9.Enabled = true;
                        try
                        {
                            button1.Text = "CapStart";
            
                            videoDevice0 = new VideoCaptureDevice(videoDevices0[comboBox8.SelectedIndex].MonikerString);
                            videoCapabilities0 = videoDevice0.VideoCapabilities;
                            comboBox9.Items.Clear();
            
                            int selected_idx = 0;
                            int idx_dsp = 0;
                            int area_max = 0;
            
                            foreach (VideoCapabilities capabilty in videoCapabilities0)
                            {
                                int r_w = capabilty.FrameSize.Width;
                                int r_h = capabilty.FrameSize.Height;
                                int area_tmp = r_w * r_h;
            
                                if (area_tmp > area_max)
                                {
                                    area_max = area_tmp;
                                    idx_dsp = selected_idx;
                                }
            
                                comboBox9.Items.Add(string.Format("{0} x {1}", r_w, r_h));
                                selected_idx++;
                            }
                            comboBox9.SelectedIndex = idx_dsp;
            
                            videoDevice0.GetCameraPropertyRange(CameraControlProperty.Exposure, out int minExp, out int maxExp, out int stepSizeExp, out int defaultExp, out controlflag0);
                            numericUpDown1.Minimum = minExp;
                            numericUpDown1.Maximum = maxExp;
                            numericUpDown1.Increment = stepSizeExp;
                            numericUpDown1.Value = defaultExp;
            
                            button1.Enabled = true;
                            button2.Enabled = true;
                            button48.Enabled = true;
                            button77.Enabled = true;
            
                            comboBox8.Enabled = true;
                        }
            
                        catch (Exception error)
                        {
                            button1.Text = error.ToString();
                        }
                    }
            
                    // dispose & reload camera system
                    private void button2_Click(object sender, EventArgs e)
                    {
                        if (web0flag > 0)
                        {
                            if (videoDevice0.IsRunning)
                            {
                                videoDevice0.SignalToStop();
                                //        videoDevice0.WaitForStop();
                                videoDevice0 = null;
                            }
                            button1.BackColor = Color.LightGray;
                            web0flag = 0;
                            button77.Text = "closed";
                            label11.ForeColor = Color.Black;
                        }
                        // webcam 1 load //
                        try
                        {
                            videoDevices0 = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            
                            if (videoDevices0.Count != 0)
                            {
                                comboBox8.Items.Clear();
                                foreach (FilterInfo device in videoDevices0)
                                {
                                    comboBox8.Items.Add(device.Name);
                                }
                                comboBox8.SelectedIndex = videoDevices0.Count - 1;
                                comboBox9.Enabled = true;
                            }
                            else
                            {
                                comboBox8.Items.Clear();
                                comboBox8.Items.Add("no devices");
                                comboBox8.SelectedIndex = 0;
                            }
                        }
                        catch (Exception error)
                        {
                            button2.Text = error.ToString();
                        }
            
            
                        comboBox8.Enabled = true;
                    }
            
                    // Dispose
                    private void button77_Click(object sender, EventArgs e)
                    {
                        videoDevice0.NewFrame -= new NewFrameEventHandler(videoDevice_NewFrame0);
                        comboBox8.Enabled = false;
                        comboBox9.Enabled = false;
                        if (web0flag > 0)
                        {
                            if (videoDevice0.IsRunning)
                            {
                                videoDevice0.SignalToStop();
                                videoDevice0.WaitForStop();
                                videoDevice0 = null;
                            }
            
                            button1.BackColor = Color.LightGray;
                            web0flag = 0;
                            button77.Text = "closed";
                            label11.ForeColor = Color.Black;
                        }
            
                        button1.Enabled = false;
                        button2.Enabled = true;
                        button48.Enabled = false;
                        button77.Enabled = false;
                    }
            
                    //CHange time
                    private void numericUpDown1_ValueChanged_1(object sender, EventArgs e)
                    {
                        videoDevice0.SetCameraProperty(CameraControlProperty.Exposure, (int)numericUpDown1.Value, controlflag0);
                    }
            
            
            
                }
            }