This article mainly describes the C # call Aforge implementation of camera video sample code, very practical value, the need for friends can refer to the following
1: First download the library file >>
can also go to the official website to find >>
Download this tutorial full code >>
Output for MP4 need to use the FFmpeg related files, I packed the library has been brought, to the official website to find the library can be found in this directory:
2: Add these references:
3: Two Global variables:
Used to operate the camera private videocapturedevice camera = null; Used to encode each frame image into a video file private videofilewriter videooutput = new Videofilewriter (); Start code://Get camera list var devs = new Filterinfocollection (filtercategory.videoinputdevice);//Instantiate Device control class (I chose 1th) camera = new Videocapturedevice (Devs[0]. monikerstring)///Configure the recording parameters (width, height, frame rate, bitrate, etc.) Videocapabilities This property returns which configurations the camera supports, selecting an assignment from here. I chose the 1th camera.videoresolution = camera.videocapabilities[0];//Set callback, Aforge will continuously eject the image data from this callback Camera.newframe + = Camera_ newframe;//Open the Camera camera.start ();//Open the video file (if it is not created, if there is also empty), here is also about Videooutput.open ("E:/video. MP4 ", Camera.VideoResolution.FrameSize.Width, Camera.VideoResolution.FrameSize.Height, Camera.videoresolution. Averageframerate, VIDEOCODEC.MPEG4, Camera.VideoResolution.BitCount); callback method for outputting image data to Aforge://Image cache private Bitmap BMP = New Bitmap (1, 1);//camera output callback private void Camera_newframe (object sender, Newframeeventargs EventArgs) {//write to File Videooutput.writevideoframe (Eventargs.frame); Lock (BMP) {//Releases the previous cache BMP. Dispose (); //Save a copy of the cache BMP = EventArgs.Frame.Clone () as Bitmap; }}
End Code:
Stop camera camera.stop (); Close the video file, if you forget not to close, you will get a corrupted file, unable to play videooutput.close ();
4: Modify app. Config, something compatible with net2.0:
<?xml version= "1.0" encoding= "Utf-8"?><configuration> <startup uselegacyv2runtimeactivationpolicy= " True "> <supportedruntime version=" v4.0 "sku=". netframework,version=v4.5 "/> </startup> <supportedruntime version=" v2.0.50727 "/></configuration >
Otherwise it would be: