1:
Download library files First >>
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:
1 // used to operate the camera 2 Private NULL ; 3 // used to encode each frame image into a video file 4 Private New Videofilewriter ();
Start code:
1 //Get camera list2 varDevs =Newfilterinfocollection (filtercategory.videoinputdevice);3 4 //instantiate the Device control class (I chose the 1th one)5Camera =NewVideocapturedevice (devs[0]. monikerstring);6 7 //Configure recording parameters (width, height, frame rate, bit rate, etc.) videocapabilities This property will return to the camera to support which configuration, from here to choose an assignment, I chose the 1th8Camera.videoresolution = camera.videocapabilities[0];9 Ten //Setting the callback, Aforge will continuously eject the image data from this callback OneCamera.newframe + =Camera_newframe; A - //Turn on the camera - Camera.start (); the - //Open the video file (if not, create it, and empty it if you have one). -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:
1 //Image Caching2 PrivateBitmap BMP =NewBitmap (1,1);3 4 //Camera Output Callback5 Private voidCamera_newframe (Objectsender, Newframeeventargs EventArgs)6 {7 //Write to File8 Videooutput.writevideoframe (eventargs.frame);9 Lock(BMP)Ten { One //Release the previous cache A bmp. Dispose (); - //Save a copy of the cache -BMP = EventArgs.Frame.Clone () asBitmap; the } -}
End Code:
// Stop the camera . camera.stop (); // Close the video file, if you forget not to close it, you will get a corrupted file that cannot be played Videooutput.close ();
4:
Modify app. config to be compatible with some of net2.0 's things:
<?XML version= "1.0" encoding= "Utf-8"?><Configuration> <StartupuseLegacyV2RuntimeActivationPolicy= "true"> <supportedruntimeversion= "v4.0"SKU=". netframework,version=v4.5 "/> </Startup> <supportedruntimeversion= "v2.0.50727"/></Configuration>
Otherwise it would be:
C # call Aforge to implement webcam recording