C#和USB红外摄像机 [英] C# and USB IR camera
问题描述
您好,我在c#中设计了一个窗口,其中包含一个组合框(包含检测到的USB设备),一个图片框(用于视频),一个标签,一个启动按钮(启动摄像头)和一个刷新按钮。
我有一个USB安全红外摄像头,我想连接到电脑,希望在图片盒中获得一些视频。
我调试并构建了项目,设计的窗口弹出应有的位置。当我连接USB相机时按下刷新按钮,它将被写入组合框USB 2.0 grabber。当我按下启动按钮时没有任何反应。有人可以帮助我/向我解释如何将视频输入到图片框中吗?我真的很感激一些帮助。
源代码如下:
使用System;
使用System.Collections.Generic;使用System.ComponentModel
;
使用System.Data;使用System.Drawing
;
使用System.Text;
使用System.Windows.Forms;
使用AForge.Video;
使用AForge.Video.DirectShow;
命名空间cam_aforge1
{
公共部分类Form1:表格
{
private bool DeviceExist = false;
private FilterInfoCollection videoDevices;
private VideoCaptureDevice videoSource = null;
public Form1()
{
InitializeComponent();
}
//获取设备名称
private void getCamList()
{
try
{
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
comboBox1.Items.Clear();
if(videoDevices.Count == 0)
抛出新的ApplicationException();
DeviceExist = true;
foreach(videoDevices中的FilterInfo设备)
{
comboBox1.Items.Add(device.Name);
}
comboBox1.SelectedIndex = 0; //使dafault转到第一个凸轮
}
catch(ApplicationException)
{
DeviceExist = false;
comboBox1.Items.Add(系统上没有捕获设备);
}
}
//刷新按钮
private void rfsh_Click(object sender,EventArgs e)
{
getCamList();
}
//切换开始和停止按钮
private void start_Click(object sender,EventArgs e)
{
if(start.Text == & Start)
{
if(DeviceExist)
{
videoSource = new VideoCaptureDevice(videoDevices [comboBox1.SelectedIndex] .MonikerString);
videoSource.NewFrame + = new NewFrameEventHandler(video_NewFrame);
064 CloseVideoSource();
videoSource.DesiredFrameSize = new Size(160,120);
//videoSource.DesiredFrameRate = 10;
videoSource.Start();
label2.Text =设备运行......;
start.Text =& Stop;
timer1.Enabled = true;
}
其他
{
label2.Text =错误:未选择任何设备。;
}
}
else
{
if(videoSource.IsRunning)
{
timer1.Enabled = false;
CloseVideoSource();
label2.Text =设备已停止。;
start.Text =& Start;
}
}
}
//如果新帧准备好,则为eventhandler
private void video_NewFrame(object sender,NewFrameEventArgs eventArgs)
{
Bitmap img =(Bitmap)eventArgs.Frame.Clone();
//在这里处理
pictureBox1.Image = img;
}
//安全关闭设备
private void CloseVideoSource()
{
if(!(videoSource == null))
if(videoSource.IsRunning)
{
videoSource.SignalToStop();
videoSource = null;
}
}
//获得1秒钟收到的总帧数
private void timer1_Tick(object sender,EventArgs e)
{
label2.Text =设备运行...+ videoSource.FramesReceived.ToString()+FPS;
}
//防止设备运行时突然关闭
private void Form1_FormClosed(object sender,FormClosedEventArgs e)
{
CloseVideoSource();
}
}
}
首先,PictureBox
对于渲染视频绝对没用。此外,渲染任何动态和交互式的东西都是无用的 - 你可以这样做,但是使用直接从System.Windows.Forms.Control
派生的自定义控件,或者只是Panel
会更容易,提供更好的性能甚至更少的编码。PictureBox
不是帮手,而是麻烦。
来自视频供稿(视频预览)来自USB摄像头,请参阅这些CodeProject文章:
Camera Vision - 视频监控C# [ ^ ](作者其他文章 Andrew Kirillov ,强烈推荐);
< a href =http://www.codeproject.com/KB/audio-video/WebcamUsingDirectShowNET.aspx>使用DirectShow.NET的网络摄像头 [ ^ ]。
你可以找到很多更多,如果你谷歌它。
-SA
这里有一些有用的解决方案:(有些可能包括在内de捕捉网络摄像头图像,但他们都显示如何让它显示在图片框中)
如何在C#中使用带有.NET Framework 4.0和Microsoft Expression Encoder 4的网络摄像头 [ ^ ]
^ ]
http://www.c-sharpcorner.com/uploadfile/youg使用C-Sharp-net-and-com-part-viii / [ ^ ] < br $> b $ b
(这是我使用的那个。我很喜欢)
Versatile WebCam C#库 [ ^
Hello, i have designed a window in c# which contains one combobox(contains detected USB devices), one picturebox(for the video), one label, one start button(starts the camera)and one refresh button.
I have a USB security IR camera which i want to connect to the computer and hopefully get some video feed in the picturebox.
I have debugged and builded the project and the designed window pops up as it should. When i push the refresh button while the USB camera is connected,it is written in the combobox "USB 2.0 grabber". When i then push the start button nothing happens. Can somebody help me/explain to me how to get the video feed into the picture box? I really appreciate some help.
Source code follows:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using AForge.Video; using AForge.Video.DirectShow; namespace cam_aforge1 { public partial class Form1 : Form { private bool DeviceExist = false; private FilterInfoCollection videoDevices; private VideoCaptureDevice videoSource = null; public Form1() { InitializeComponent(); } // get the devices name private void getCamList() { try { videoDevices = new FilterInfoCollection (FilterCategory.VideoInputDevice); comboBox1.Items.Clear(); if (videoDevices.Count == 0) throw new ApplicationException(); DeviceExist = true; foreach (FilterInfo device in videoDevices) { comboBox1.Items.Add(device.Name); } comboBox1.SelectedIndex = 0; //make dafault to first cam } catch (ApplicationException) { DeviceExist = false; comboBox1.Items.Add("No capture device on your system"); } } //refresh button private void rfsh_Click(object sender, EventArgs e) { getCamList(); } //toggle start and stop button private void start_Click(object sender, EventArgs e) { if (start.Text == "&Start") { if (DeviceExist) { videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString); videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame); 064 CloseVideoSource(); videoSource.DesiredFrameSize = new Size(160,120); //videoSource.DesiredFrameRate = 10; videoSource.Start(); label2.Text = "Device running..."; start.Text = "&Stop"; timer1.Enabled = true; } else { label2.Text = "Error: No Device selected."; } } else { if (videoSource.IsRunning) { timer1.Enabled = false; CloseVideoSource(); label2.Text = "Device stopped."; start.Text = "&Start"; } } } //eventhandler if new frame is ready private void video_NewFrame(object sender, NewFrameEventArgs eventArgs) { Bitmap img = (Bitmap)eventArgs.Frame.Clone(); //do processing here pictureBox1.Image = img; } //close the device safely private void CloseVideoSource() { if (!(videoSource == null)) if (videoSource.IsRunning) { videoSource.SignalToStop(); videoSource = null; } } //get total received frame at 1 second tick private void timer1_Tick(object sender, EventArgs e) { label2.Text = "Device running... " + videoSource.FramesReceived.ToString() + " FPS"; } //prevent sudden close while device is running private void Form1_FormClosed(object sender, FormClosedEventArgs e) { CloseVideoSource(); } } }
First of all,PictureBox
is absolutely useless for rendering video. Moreover, it's quite useless for rendering anything dynamic and interactive — you could do it, but using a custom control derived directly fromSystem.Windows.Forms.Control
or justPanel
would be much easier and provide better performance and even less coding. ThePictureBox
is not a helper but rather a hassle here.
For the video feed (video preview) from USB camera, see, for example, these CodeProject articles:
Camera Vision - video surveillance on C#[^] (see other articles by its author Andrew Kirillov, highly recommended);
Webcam using DirectShow.NET[^].
You can find a lot more if you Google for it.
—SA
Here are some helpful solutions: (Some may include capturing webcam images but they all show ways to get it to show in the picture box)
How to use a web cam in C# with .NET Framework 4.0 and Microsoft Expression Encoder 4[^]
Webcam in C#: Easiest Way to Capture Images from Your Webcams[^]
http://www.c-sharpcorner.com/uploadfile/yougerthen/integrate-the-web-webcam-functionality-using-C-Sharp-net-and-com-part-viii/[^]
(This is the one I use. I like it)
Versatile WebCam C# library[^]
这篇关于C#和USB红外摄像机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!