C # Implementation invokes native camera instance

Source: Internet
Author: User

The example of this article originates from a project, which needs to call the camera of this machine to take pictures and share it for everyone's reference. The steps are as follows:

Hardware environment: Lenovo C360 integrated machine with camera

Authoring Environment: VS2010

Language: C # WPF


Implementation steps:

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, here you can make flexible changes 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 (stream);}  }

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;

I hope this article is helpful to everyone's C # programming.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
C # Implementation invokes native camera instance

This address: http://www.paobuke.com/develop/c-develop/pbk23585.html






Related content C # implementation of small screenshot software C # using the Irawpixels interface to traverse Raster Data C # Implementation of JSON string serialization and deserialization method C # use regular to determine whether the input is a pure number, container class
C # Scripting ActiveX Web page controls how to get file icons in C # common C # Regular Expression Rollup Introduction to asynchronous callback function usages in C #

C # Implementation invokes native camera instance

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.