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;
//Create using directives for easier access of AForge library's methods
using
AForge.Video;
using
AForge.Video.DirectShow;
namespace
aforgeWebcamTutorial
{
public
partial
class
Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
//Create webcam object
VideoCaptureDevice videoSource;
private
void
Form1_Load(
object
sender, EventArgs e)
{
//List all available video sources. (That can be webcams as well as tv cards, etc)
FilterInfoCollection videosources =
new
FilterInfoCollection(FilterCategory.VideoInputDevice);
//Check if atleast one video source is available
if
(videosources !=
null
)
{
//For example use first video device. You may check if this is your webcam.
videoSource =
new
VideoCaptureDevice(videosources[0].MonikerString);
try
{
//Check if the video device provides a list of supported resolutions
if
(videoSource.VideoCapabilities.Length > 0)
{
string
highestSolution =
"0;0"
;
//Search for the highest resolution
for
(
int
i = 0; i < videoSource.VideoCapabilities.Length; i++) {
if
(videoSource.VideoCapabilities[i].FrameSize.Width > Convert.ToInt32(highestSolution.Split(
';'
)[0]))
highestSolution = videoSource.VideoCapabilities[i].FrameSize.Width.ToString() +
";"
+ i.ToString();
}
//Set the highest resolution as active
videoSource.VideoResolution = videoSource.VideoCapabilities[Convert.ToInt32(highestSolution.Split(
';'
)[1])];
}
}
catch
{ }
//Create NewFrame event handler
//(This one triggers every time a new frame/image is captured
videoSource.NewFrame +=
new
AForge.Video.NewFrameEventHandler(videoSource_NewFrame);
//Start recording
videoSource.Start();
}
}
void
videoSource_NewFrame(
object
sender, AForge.Video.NewFrameEventArgs eventArgs)
{
//Cast the frame as Bitmap object and don't forget to use ".Clone()" otherwise
//you'll probably get access violation exceptions
pictureBoxVideo.BackgroundImage = (Bitmap)eventArgs.Frame.Clone();
}
private
void
Form1_FormClosed(
object
sender, FormClosedEventArgs e)
{
//Stop and free the webcam object if application is closing
if
(videoSource !=
null
&& videoSource.IsRunning)
{
videoSource.SignalToStop();
videoSource =
null
;
}
}
}
}
Hiç yorum yok:
Yorum Gönder