如何在图片框上显示网络摄像头图像

时间:2019-07-18 02:52:57

标签: c# logitech

我正在编写C#代码,此代码可以将Logitech USB c525网络摄像头显示到图片框。 我使用AFORGE / OpenCvSharp / avicap32.dll从Web尝试了许多示例代码,但均未成功,在picturebox上不显示任何图像。 网络摄像头C525与Win10中的摄像头应用程序等其他应用程序配合使用,因此网络摄像头可以正常工作,而不会损坏。

您能看看我的代码中我缺少什么吗?

如果我在笔记本电脑上使用默认网络摄像头,则可以很好地与我的代码配合使用

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

namespace LogitechWebCam
{
    public partial class Form1 : Form
    {
        //Create webcam object
        VideoCaptureDevice videoSource;

        private FilterInfoCollection VideoCaptureDevices;
        private VideoCaptureDevice FinalVideo;

        public void AForgeLoadDevice()
        {
            VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            foreach (FilterInfo VideoCaptureDevice in VideoCaptureDevices)
            {
                comboBox1.Items.Add(VideoCaptureDevice.Name);
            }
            comboBox1.SelectedIndex = 0;
        }

        public void AForgeOpen()
        {
            FinalVideo = new VideoCaptureDevice(VideoCaptureDevices[comboBox1.SelectedIndex].MonikerString);
            FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
            FinalVideo.Start();
        }

        public void AForgeClose()
        {
            FinalVideo.Stop();
        }


        void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            Bitmap video = (Bitmap)eventArgs.Frame.Clone();
            pictureBox1.Image = video;

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

namespace LogitechWebCam
{
    public partial class Form1 : Form
    {
        LogiWebCam webCam;
        WebCamera wc;

        public Form1()
        {
            InitializeComponent();
        }

        // start cam
        private void Button1_Click(object sender, EventArgs e)
        {
            AForgeOpen();
        }

        // stop cam
        private void Button2_Click(object sender, EventArgs e)
        {
            AForgeClose();
        }

        // load cam
        private void Button3_Click(object sender, EventArgs e)
        {
            AForgeLoadDevice();
        }
    }
}

构建正常,当我运行程序时,我可以从诊断窗口看到内存增加,并且可以看到C525网络摄像头已启动,因此网络摄像头确实可以正常工作,但无法在图片框屏幕上显示图像。

1 个答案:

答案 0 :(得分:0)

很抱歉... 现在可以按我的预期在这里工作了。 这是由于USB集线器问题。 我可以确认此代码可以正常工作。

相关问题