一个页面显示两个视频

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值