An article about aforge.net specific use and attention to the place.
1, get all the cameras on the device:
private void Mainwindow_loaded (object sender, RoutedEventArgs e)
{
Set the initial video device
Videodevices = new Filterinfocollection (filtercategory.videoinputdevice);
Xcount = Videodevices.count;
if (Videodevices.count > 0)
{//Default device
Sourceplayer.videosource = new Videocapturedevice (videodevices[0). monikerstring);
}
Sourceplayer.start ();
Set Picture frame initial image
BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap (
Properties.Resources.noimage.GetHbitmap (),
IntPtr.Zero,
Int32rect.empty,
Bitmapsizeoptions.fromemptyoptions ());
Fingerpicturebox1.initialimage = BS;
if (sourceplayer.isrunning)
{
Button_capture.isenabled = true;
}
}
The sourceplayer.isrunning indicates whether the camera is working.
2, multiple camera switching:
<summary>
Switch camera
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Button_qh_click (object sender, RoutedEventArgs e)
{
if (J < xcount-1)
{
j = j + 1;
}
else if (j = = xcount-1)
{
j = 0;
}
Sourceplayer.stop ();
Sourceplayer.videosource = new Videocapturedevice (videodevices[j). monikerstring);
Sourceplayer.start ();
}
3, taking photos:
<summary>
Taking images
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Button_capture_click (object sender, RoutedEventArgs e)
{
Determine if the video device is turned on
Try
{
if (sourceplayer.isrunning)
{//Take photos
if (fingerpicturebox1.visibility = = Visibility.hidden)
{
Object box = this. FindName ("FingerPictureBox1");
if (box is Fingerpicturebox)
{
(Box as Fingerpicturebox). Activeimage = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap (
Sourceplayer.getcurrentvideoframe (). Gethbitmap (),
IntPtr.Zero,
Int32rect.empty,
Bitmapsizeoptions.fromemptyoptions ());
}
fingerpicturebox1.visibility = visibility.visible;
Cam_sumit. Visibility = Visibility.hidden;
Lb_text. Content = "Launch Camera";
}
else {
fingerpicturebox1.visibility = Visibility.hidden;
Cam_sumit. Visibility = visibility.visible;
Lb_text. Content = "shooting image";
}
}
}
catch {
MessageBox.Show ("Please wait until the camera is ready to take pictures!") ");
}
}
4. Save and return:
<summary>
Save return
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Button_close_click (object sender, RoutedEventArgs e)
{
var tempfilepath = "";
RenderTargetBitmap bmp = New RenderTargetBitmap (
(int) fingerpicturebox1.actualwidth, (int) fingerpicturebox1.actualheight,
Pixelformats.default);
Fingerpicturebox1.measure (fingerpicturebox1.rendersize);
Fingerpicturebox1.arrange (New Rect (fingerpicturebox1.rendersize));
Bmp. Render (FingerPictureBox1);
Bitmapencoder encoder = new Jpegbitmapencoder ();
Encoder. Frames.add (Bitmapframe.create (BMP));
using (MemoryStream ms = new MemoryStream ())
{
Encoder. Save (MS);
Capturedata = Ms. ToArray ();
var TempPath = AppDomain.CurrentDomain.BaseDirectory + DateTime.Now.ToString ("yyyy-mm-dd-hh-mm-sss") + ". jpg";
File.writeallbytes (TempPath, capturedata);
TempFilePath = Fileoperation.addfile (Common.Contract.WebModel.AyncModuleEnum. System photo, TempPath, true);
}
if (cameradoevent!= null)
{
Cameradoevent (TempFilePath);
}
This. Close ();
}
5. The camera must be turned off when the window is closed, or the camera will remain open:
private void Window_closing (object sender, System.ComponentModel.CancelEventArgs e)
{
if (sourceplayer.isrunning)
{//Stop video
Sourceplayer.signaltostop ();
Sourceplayer.waitforstop ();
Sourceplayer.stop ();
}
}