- Code: Select all
public WebCamHelper()
{
_userPreferences = UserPreferences.Current;
SetDevices();
}
public void SetDevices()
{
SetDevices(
_userPreferences.WebCamDeviceID,
_userPreferences.WebCamResolution
);
}
public void SetDevices(int DeviceID, string Resolution)
{
try
{
videoSource = new VideoCaptureDevice(videosources[DeviceID].MonikerString);
try
{
//Check if the video device provides a list of supported resolutions
if (videoSource.VideoCapabilities.Length > 0)
{
for (int i = 0; i < videoSource.VideoCapabilities.Length; i++)
{
if ((videoSource.VideoCapabilities[i].FrameSize.Width + " x " + videoSource.VideoCapabilities[i].FrameSize.Height) == Resolution)
{
videoSource.VideoResolution = videoSource.VideoCapabilities[i];
break;
}
}
}
}
catch
{
//if can't set selected resolution, choose lowest available
if (videoSource.VideoCapabilities.Length > 0)
{
string lowestSolution = "0;0";
//Search for the lowest resolution (can be changed to highest by changing [<] in line 74 to [>] and renaming variables lowestSolution -> highestSolution
for (int i = 0; i < videoSource.VideoCapabilities.Length; i++)
{
if (videoSource.VideoCapabilities[i].FrameSize.Width < Convert.ToInt32(lowestSolution.Split(';')[0]))
lowestSolution = videoSource.VideoCapabilities[i].FrameSize.Width.ToString() + ";" + i.ToString();
}
//Set the resolution as active
videoSource.VideoResolution = videoSource.VideoCapabilities[Convert.ToInt32(lowestSolution.Split(';')[1])];
}
}
videoSource.NewFrame += new AForge.Video.NewFrameEventHandler(videoSource_NewFrame);
}
catch (Exception ex)
{
//no-removed webcam ?
Main cm = (Main)System.Windows.Forms.Application.OpenForms["Main"];
cm.DisableWebCamSettingsMenu();
}
}
void videoSource_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
{
OnFrameReady((Bitmap)eventArgs.Frame.Clone(), null);
}
public void Start()
{
videoSource.Start();
}
public void Stop()
{
if (videoSource != null && videoSource.IsRunning)
{
videoSource.SignalToStop();
}
}