c#使用Aforge库在网络摄像头流上绘制线条

时间:2016-11-27 16:39:45

标签: c# webcam aforge

我是初学c#程序员。我需要开发一个软件,使用网络摄像头视频流上的2个不同按钮绘制绿色和红色线条。我设法流式传输视频并使用Aforge.net库绘制一条线。

但在完成一行后按下按钮时,第一行消失。

public f21()
    {
        InitializeComponent();


        blobCounter.MinWidth = 2;
        blobCounter.MinHeight = 2;
        blobCounter.FilterBlobs = true;
        blobCounter.ObjectsOrder = ObjectsOrder.Size;
        try
        {
            // enumerate video devices
            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

            if (videoDevices.Count == 0)
                throw new ApplicationException();

            // add all devices to combo
            foreach (FilterInfo device in videoDevices)
            {
                camerasCombo.Items.Add(device.Name);
            }

            camerasCombo.SelectedIndex = 0;
        }
        catch (ApplicationException)
        {
            camerasCombo.Items.Add("No local capture devices");
            videoDevices = null;
        }



    }
    private void f21_Load(object sender, EventArgs e)
    {
    }
    private void button1_Click(object sender, EventArgs e)
    {


        videoSourcePlayer2.SignalToStop();
        videoSourcePlayer2.WaitForStop();

        // videoDevices = null;
        VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[camerasCombo.SelectedIndex].MonikerString);
        videoSource.DesiredFrameSize = new Size(640, 480);
        videoSource.DesiredFrameRate = 12;


        videoSourcePlayer2.VideoSource = videoSource;
        videoSourcePlayer2.Start();

        //groupBox1.Enabled = false;
    }



    private void button3_Click_1(object sender, EventArgs e)
    {
        x = 1;
        Y = 0;
    }

    private void button4_Click(object sender, EventArgs e)
    {
        x = 0;
        Y = 1;
    }
    private void videoSourcePlayer2_MouseDown(object sender, MouseEventArgs e)
    {
        if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
        {
            // Remember the location where the button was pressed
            pt1 = e.Location;
            pt2 = new Point(e.X, 0);
        }
    }
    private void videoSourcePlayer2_Click(object sender, EventArgs e)
    {


        if (x == 1)
        {
            click = 1;
        }

        if (Y == 1)
        {
            click = 2;
        }


    }
    private void videoSourcePlayer2_NewFrame(object sender, ref Bitmap image)
    {


        Pen Green = new Pen(Color.Green, 3);
        Pen Greenb = new Pen(Color.ForestGreen, 6);
        Pen Red = new Pen(Color.Red, 3);
        Pen Redb = new Pen(Color.DarkRed, 6);
        Graphics g1 = Graphics.FromImage(image);

        if (click == 1)
        {

            g1.DrawLine(Green, pt1, pt2);

        }

        if (click == 2)
        {

            g1.DrawLine(Red, pt1, pt2);
        }
        g1.Dispose();

    }

任何宗师都可以为此启发我吗?

0 个答案:

没有答案