gpt4 book ai didi

c# - 如何初始化AForge网络摄像头

转载 作者:行者123 更新时间:2023-11-30 13:59:38 25 4
gpt4 key购买 nike

我尝试编写少量代码来使用 Aforge 捕获帧我引用了 Aforge.dll 和 AForge.Video.DirectShow.dll代码在下面,但我做错了什么。我得到的警告“名称 videoDevices 在当前上下文中不存在。我认为这与我尝试创建该变量的位置有关,但我不确定将按钮代码放置在何处以对其进行初始化。该错误在 visual studio 中也显示为对象“videoDevices”下的红线

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AForge;
using AForge.Video.DirectShow;

namespace AforgeCam
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)
{

videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

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

foreach (FilterInfo device in videoDevices)
{
VideoCaptureDevice videoSource = new VideoCaptureDevice(device.MonikerString);
videoSource.DesiredFrameSize = new Size(320, 240);
videoSource.DesiredFrameRate = 15;
videoSourcePlayer1.VideoSource = videoSource;
videoSourcePlayer1.Start();
}

}
}
}

最佳答案

根据要求,解决方案如下,代码有效,我将针对另一个问题提出一个新问题。该代码需要一个下拉框、两个按钮和一个图片框

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AforgeCam;
using AForge.Video;
using AForge.Video.DirectShow;

namespace AforgeCam
{
public partial class Form1 : Form
{
private FilterInfoCollection VideoCaptureDevices;
private VideoCaptureDevice FinalVideo;

public Form1() // init
{
InitializeComponent();
{
VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo VideoCaptureDevice in VideoCaptureDevices)
{
comboBox1.Items.Add(VideoCaptureDevice.Name);
}
comboBox1.SelectedIndex = 0;
}
}

private void button1_Click(object sender, EventArgs e)
{
FinalVideo = new VideoCaptureDevice(VideoCaptureDevices[comboBox1.SelectedIndex].MonikerString);
FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
FinalVideo.Start();
}

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

}

private void button2_Click(object sender, EventArgs e)
{
FinalVideo.Stop();
}
}

关于c# - 如何初始化AForge网络摄像头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12925021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com