問題描述
我嘗試使用 Aforge 編寫一小段代碼來捕獲幀我參考了 Aforge.dll 和 AForge.Video.DirectShow.dll代碼如下,但我做錯了什么.警告我得到名稱 videoDevices 在當前上下文中不存在.我認為這與我嘗試創(chuàng)建該變量的位置有關,但我不確定在哪里放置按鈕的代碼以使其初始化.錯誤在 Visual Studio 中也顯示為對象videoDevices"下的紅線
I try to write a small peace of code to capture a frame using Aforge I made a reference to Aforge.dll and AForge.Video.DirectShow.dll The code is below, but i am doing something wrong. The Warning i get "the name videoDevices does not exist in the current context. I think it has to do about where i try to create that variable but i'm not exactly sure as where to place that code of the button to get it initialized. The error is displayed in visual studio also as a redline under the object "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();
}
}
}
}
推薦答案
根據(jù)要求,解決方案如下,代碼有效,我將針對我對此提出的另一個問題提出一個新問題.該代碼需要一個下拉框、2個按鈕和一個圖片框
As requested the solution is below , the code works, i will raise a new question for another question i have about it. the code requires a dropdown box, 2 buttons and a picturebox
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();
}
}
這篇關于如何初始化 AForge 網絡攝像頭的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!