Aforge.net use of video camera function realization

Source: Internet
Author: User

Original address: http://blog.csdn.net/chenhongwu666/article/details/41965801

Connect the camera device, you need to introduce

Aforge.video;

AForge.Video.DirectShow;

AForge.Video.FFMPEG;

You also need to add a reference, Aforge.dll,aforge.video,aforge.video.directshow,aforge.control,aforge.video.ffmpeg,

You also need to add Aforge.control in the Toolbox, and then find Videosourceplayer this control to add to the interface

Then define the variables

Private Filterinfocollection videodevices;
Private Videocapturedevice VideoSource;

private bool Stoprec = true;
private bool CreateNewFile = true;

private string Videofilefullpath = String. Empty; Video File Full path
private string Imagefilefullpath = String. Empty; Image file Full path
private string Videopath = @ "E:\video\"; Video file path
private string ImagePath = @ "E:\video\images\"; Image file path
private string videofilename = String. Empty; Video file name
private string imagefilename = String. Empty; Image file name
private string drawdate = String. Empty;
Private Videofilewriter videowriter = null;

public delegate void Myinvoke (); Defining a Delegate method

String G_s_autosavepath = AppDomain.CurrentDomain.BaseDirectory + "capture\\";
Object Objlock = new Object (); Defines a lock for an object
int framerate = 20; Default frame rate
Private Stopwatch Stopwatch = null;
IVideoSource ivideosource = null;

 private  void   Initui () { //  Connect  if   (MSC. Openport ()) {MSc.        Connectmicroscopecontrol ();  //  turn on camera  videodevices = VH.        GetDevices ();  if  (videodevices! = null  & & Videodevices.count > 0  ) {VI Deosource  = VH.        Videoconnect ();        } videosourceplayer1.videosource  = VideoSource;    Videosourceplayer1.start (); }}

Start recording

 Private voidBtnstartvideotape_click (Objectsender, EventArgs e) {            //Start Recording            if(Btnstartvideotape.text = ="Start Recording") {Stoprec=false; Framerate=Convert.ToInt32 (TxtFrameRate.Text.Trim ()); Btnstartvideotape.text="Stop Recording"; }            Else if(Btnstartvideotape.text = ="Stop Recording") {Stoprec=true; Btnstartvideotape.text="Start Recording"; }        }

After adding a Videosourceplayer control for Aforge.net, find the Newframe event with the following code:

Here is an event for the control, which is the code for the real recording

Private voidVideosourceplayer1_newframe (ObjectSenderrefBitmap image) {            //VideoGraphics g =graphics.fromimage (image); SolidBrush Drawbrush=NewSolidBrush (Color.yellow); Font Drawfont=NewFont ("Arial",6, FontStyle.Bold, Graphicsunit.millimeter); intXPos = image. Width-(image. Width- the); intYPos =Ten; //time to write on the screenDrawdate = DateTime.Now.ToString ("YYYY-MM-DD HH:mm:ss");            g.DrawString (Drawdate, Drawfont, Drawbrush, XPos, YPos); if(!directory.exists (Videopath))            Directory.CreateDirectory (Videopath); //Create file path//Filefullpath = path + fileName;            if(Stoprec) {Stoprec=true; CreateNewFile=true;//This is set to true to indicate that you want to create a new file                if(Videowriter! =NULL) Videowriter.close (); }            Else            {                //Start Recording                if(createnewfile) {videofilename= DateTime.Now.ToString ("yyyy. MM.DD HH.MM.SS") +". avi"; Videofilefullpath= Videopath +Videofilename; CreateNewFile=false; if(Videowriter! =NULL) {videowriter.close ();                    Videowriter.dispose (); } videowriter=NewVideofilewriter (); //This must be a full path, otherwise it will be saved by default to the program run according to the recordedVideowriter.open (Videofilefullpath, image. Width, image.                    Height, framerate, VIDEOCODEC.MPEG4);                Videowriter.writevideoframe (image); }                Else{videowriter.writevideoframe (image); }            }        }

Photo Code

/// <summary>///manual photo or grab image/// </summary>/// <param name= "Sender" ></param>/// <param name= "E" ></param>Private voidBtncapture_click (Objectsender, EventArgs e) {    Try    {        intNumber=0; number++; stringFileimagename = G_s_requestno +"-"+ number +". bmp"; stringFilecapturepath = G_s_autosavepath + G_s_requestno +"\\"; if(!directory.exists (Filecapturepath))        Directory.CreateDirectory (Filecapturepath); //capture diagram save to specified pathBitmap BMP =NULL; BMP=Videosourceplayer1.getcurrentvideoframe (); if(BMP = =NULL) {MessageBox.Show ("failed to capture image! ","Tips"); return; } BMP. Save (Filecapturepath+Fileimagename, imageformat.bmp); }    Catch(Exception ex) {MessageBox.Show ("failed to capture image! "+ ex. Message,"Tips"); }}

The most basic work is completed, if you want more perfect words also need refinement, optimization and so on, first recorded this, in order to record their own little bit of progress

Aforge.net use of video camera function realization

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.