C # call camera to take pictures

Source: Internet
Author: User

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

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.