一个页面显示两个视频

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 OpenCvSharp;
using OpenCvSharp.Extensions;
using System.Threading;

namespace app01
{
public partial class Form1 : Form
{
Bitmap result1 = null;
Bitmap result2 = null;
void readcamera1()
{
Mat src = new Mat();
Mat frame1 = new Mat();
Mat frame2 = new Mat();
Mat dst = new Mat();
Mat gray = new Mat();

        //FrameSource frame = Cv2.CreateFrameSource_Video("video.mp4");// 
        FrameSource carmera1 = Cv2.CreateFrameSource_Camera(0);
        carmera1.NextFrame(src);
        while (true)
        {
            frame1 = src;
            carmera1.NextFrame(src);
            frame2 = src;
            OpenCvSharp.Size size = new OpenCvSharp.Size(pictureBox1.Width,pictureBox1.Height);

            Cv2.Resize(frame1, frame1, new OpenCvSharp.Size(pictureBox1.Width, pictureBox1.Height));
            //Cv2.Canny(src, dst, 90,150);
            Cv2.Flip(frame1, frame1, FlipMode.Y);
            Cv2.CvtColor(frame1, gray, ColorConversionCodes.BGR2GRAY);
            Cv2.Threshold(gray, dst, 90, 120, ThresholdTypes.Binary);

            Bitmap bitmap = BitmapConverter.ToBitmap(frame1);


            Invalidate();
             pictureBox1.Invalidate();
            
            //Thread.Sleep(10);
            result1 = bitmap;

        }
    }
    void readcamera2()
    {
        Mat src = new Mat();
        Mat frame1 = new Mat();
        Mat frame2 = new Mat();
        Mat dst = new Mat();
        Mat gray = new Mat();

        //FrameSource frame = Cv2.CreateFrameSource_Video("video.mp4");// 
        try
        {
            FrameSource carmera1 = Cv2.CreateFrameSource_Video("video.mp4");
            //FrameSource carmera1 = Cv2.CreateFrameSource_Camera(1);
            carmera1.NextFrame(src);
            while (true)
            {
                frame1 = src;
                carmera1.NextFrame(src);
                frame2 = src;
                OpenCvSharp.Size size = new OpenCvSharp.Size(pictureBox1.Width, pictureBox1.Height);

                Cv2.Resize(frame1, frame1, new OpenCvSharp.Size(pictureBox2.Width, pictureBox2.Height));
                //Cv2.Canny(src, dst, 90,150);
                Cv2.Flip(frame1, frame1, FlipMode.Y);
                Cv2.CvtColor(frame1, gray, ColorConversionCodes.BGR2GRAY);
                Cv2.Threshold(gray, dst, 90, 120, ThresholdTypes.Binary);

                Bitmap bitmap = BitmapConverter.ToBitmap(frame1);


                Invalidate();
                  pictureBox2.Invalidate();
               // Thread.Sleep(10);

                result2 = bitmap;

            }
        }
        
        catch(OpenCvSharp.OpenCVException e)
        {

        }
        
    }
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.pictureBox1.Paint += pictureBox1_Paint;
        this.pictureBox2.Paint += pictureBox1_Paint;
    }



    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        if (true)
        {


            // Invalidate();
            //  pictureBox1.Invalidate();
            
            if ((object)sender == pictureBox1)
           
            {
                if (result1 == null)
                {

                }
                else
                {
                    //pictureBox1.Invalidate();
                    e.Graphics.DrawImage(result1, 0, 0);
                }
            }
            if ((object)sender == pictureBox2)

            {
                if (result2 == null)
                {

                }
                else
                {
                    //pictureBox1.Invalidate();
                    e.Graphics.DrawImage(result2, 0, 0);
                }
            }
            //pictureBox1.Invalidate();

        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Thread threadA = new Thread(readcamera1);
        //threadA.Start();
        Thread threadB = new Thread(readcamera2);
        threadA.Start();
        threadB.Start();
    }
}

}

猜你喜欢

转载自blog.csdn.net/tel_1392/article/details/113823203