C # calls the native camera

Source: Internet
Author: User

During this time, a small project needs to call the camera to take pictures, collect some information on the net and solve some minor problems, this record is used for subsequent use.

Hardware environment: Lenovo C360 integrated machine with camera

Authoring Environment: VS2010

Language: C # WPF

Download the Aforge class library and add references:

Using aforge;using aforge.controls;using aforge.video;using aforge.video.directshow;using Size = System.Drawing.Size;

Add the Videosourceplayer control in the XAML interface, which explains slightly how to add foreign controls:

Add a new tab in the Toolbox, right-click to add a selection, browse select control DLL OK, reference control can be added to the toolbox.

Enumerate all the cameras:

Filterinfocollection videodevices;videodevices = new Filterinfocollection (filtercategory.videoinputdevice); foreach (FilterInfo device in videodevices)                {                    //can make processing                }

Connect the camera:

Statement: Fileterinfo info;
info = videodevices[0];//Select the first one, where flexible changes can be made

Videocapturedevice VideoSource = new Videocapturedevice (videodevices[info. monikerstring); Videosource.desiredframesize = new System.Drawing.Size (214, 281); Videosource.desiredframerate = 1; Videosourceplayer.videosource = VideoSource; Videosourceplayer.start ();

Turn off the camera:

Videosourceplayer.signaltostop ();            Videosourceplayer.waitforstop ();

Photo:

 if (videosourceplayer.isrunning) {
String path = "E:\" BitmapSource BitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap ( Videosourceplayer.getcurrentvideoframe (). Gethbitmap (), IntPtr.Zero, Int32rect.empty, Bitmapsizeoptions.fromemptyoptions ()); Pngbitmapencoder PE = new Pngbitmapencoder (); PE.FRAMES.ADD (Bitmapframe.create (BitmapSource)); String picname = path + "Paizhao" + ". jpg"; if (file.exists (Picname)) {file.delete (picname); } using (Stream stream = File.create (picname)) {Pe.save (stre AM); } }

The project requires that the camera is in the monitoring state, after the picture fixed storage, not satisfied can be emptied again to take pictures, until satisfied.

The idea is to add an image control on top of Videosourceplayer because the project is made by WPF, all photos display can only add an image control, there are two points to note:

1) WPF references the WinForm control requires the use of the WindowsFormsHost control, so monitoring video and photo display is the display and hiding of control windowsformshost and image controls, so take a detour and record it.

2) The source of the image control is already bound, but the photo needs to be emptied to delete the photo resource, which means that the resource is already occupied and cannot be deleted. Solution path:

Statement: BitmapImage BMI = new System.Windows.Media.Imaging.BitmapImage ();

When used: BMI. Bgeininit ();

Bmi. UriSource = new Uri (picname);

Bmi. cacheoption = Bitmapcacheoption.onload;

Bmi. EndInit ();

Binding: This.image.Source = BMI;

C # calls the native camera

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.