In the Winforn program, you will often encounter some of the functions of calling the hardware, here to explain things to use aforge call camera
First reference with DLL file
These are the DLL files that need to be applied, in which the AForge.Controls.dll file encapsulates some void controls, and the application file in the Toolbox appears as follows
This will use the Videosourceplayer control, below the underlying code
Using reference
Using System.Drawing.Imaging;
Using System.Windows;
Using System.IO;
Using System.Windows.Media.Imaging;
Using Aforge;
Using Aforge.controls;
Using Aforge.video;
Using AForge.Video.DirectShow;
Privatefilterinfocollection videodevices;PrivateVideocapturedevice VideoSource; PublicFrmoperatecamera () {InitializeComponent (); } /// <summary> ///Form Loading/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidFrmoperatecamera_load (Objectsender, EventArgs e) { Try { //Enumerate all video input devicesVideodevices =Newfilterinfocollection (Filtercategory.videoinputdevice); if(Videodevices.count = =0) Throw NewApplicationException (); foreach(FilterInfo deviceinchvideodevices) {TSCBXCAMERAS.ITEMS.ADD (device. Name); } Tscbxcameras.selectedindex=0; } Catch(applicationexception) {TSCBXCAMERAS.ITEMS.ADD ("No Local capture devices"); Videodevices=NULL; } } /// <summary> ///Connection/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidBtnconnect_click (Objectsender, EventArgs e) {Cameraconn (); } //Connecting the camera Private voidCameraconn () {Videocapturedevice VideoSource=NewVideocapturedevice (Videodevices[tscbxcameras.selectedindex]. monikerstring); Videosource.desiredframesize=NewSystem.Drawing.Size ( the, -); Videosource.desiredframerate=1; Videosourceplayer.videosource=VideoSource; Videosourceplayer.start (); } /// <summary> ///Close/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidbtnClose_Click (Objectsender, EventArgs e) {videosourceplayer.signaltostop (); Videosourceplayer.waitforstop (); } /// <summary> ///Take Pictures/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidPhotograph_click (Objectsender, EventArgs e) { Try { if(videosourceplayer.isrunning) {BitmapSource BitmapSource=System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap (videosourceplayer.ge Tcurrentvideoframe (). Gethbitmap (), IntPtr.Zero, Int32rect.empty, Bitmapsizeoptions.fromemptyoptions ()); Pngbitmapencoder PE=NewPngbitmapencoder (); PE.FRAMES.ADD (Bitmapframe.create (BitmapSource)); stringPA = DateTime.Now.ToString ("YYYYMMDDHHMMSS"); Random Rd=NewRandom (); PA+ = Rd. Next (9); stringPicname = Getimagepath () +"\\"+ PA +". jpg"; if(File.exists (picname)) {file.delete (picname); } using(Stream stream =file.create (Picname)) {Pe.save (stream); } //close the camera and refresh the same closing form when you are done taking pictures if(Videosourceplayer! =NULL&&videosourceplayer.isrunning) {videosourceplayer.signaltostop (); Videosourceplayer.waitforstop (); } This. Close (); } } Catch(Exception ex) {MessageBox.Show ("Camera Exception:"+Ex. Message); } } Private stringGetimagepath () {stringPersonimgpath =path.getdirectoryname (AppDomain.CurrentDomain.BaseDirectory)+ Path.DirectorySeparatorChar.ToString () +"personimg"; if(!directory.exists (Personimgpath)) {directory.createdirectory (Personimgpath); } returnPersonimgpath; }
View Code
Here also need to add some program reference, otherwise will error, Windowsbase,system.xaml,presentationcore.
The form effect is as follows, and the display section uses the Videosourceplayer control.
The above is the actual effect.
C # call camera to take pictures