C # Example of using Aforge to implement camera recording capabilities

Source: Internet
Author: User
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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.