C # Call the local camera

Source: Internet
Author: User

During this time, a small project needs to call the camera of the local machine to take photos. Some information is collected on the Internet and some minor problems are solved, which are recorded here for later use.

Hardware environment: Lenovo c360 all-in-one machine with built-in camera

Compiling environment: vs2010

Language: C # WPF

 

Download the aforge class library and add reference:

using AForge;using AForge.Controls;using AForge.Video;using AForge.Video.DirectShow;using Size = System.Drawing.Size;
View code

 

Add the videosourceplayer control on the XAML interface. This section explains how to add a foreign control:

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

 

Enumerate all cameras:

Filterinfocollection videodevices; videodevices = new filterinfocollection (filtercategory. videoinputdevice); foreach (filterinfo device in videodevices) {// processing can be performed}

 

Connect the camera:

Declaration: fileterinfo Info;
Info = videodevices [0]; // select the first one, which can be changed flexibly.

Videocapturedevice videosource = new videocapturedevice (videodevices [info. monikerstring); videosource. desiredframesize = new system. drawing. size (214,281); videosource. desiredframerate = 1; videosourceplayer. videosource = videosource; videosourceplayer. start ();

 

Disable the camera:

videoSourcePlayer.SignalToStop();            videoSourcePlayer.WaitForStop();

 

Photograph:

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 status. After taking the photo, the screen is fixed for storage. If you are not satisfied, you can clear it and take the photo again until you are satisfied.

The method is to add an image control on videosourceplayer. Because the project is made by WPF, only the image control can be added for all photo displays. Note the following two points:

1) The windowsformshost control must be used to reference the winform control in WPF. Therefore, the windowsformshost and image controls are used to display and hide the monitoring videos and photos. A detour is taken here to record them.

2) The source of the image control has been bound, but the photo needs to be cleared and deleted. The system prompts that the resource has been occupied and cannot be deleted. Solution:

Statement: bitmapimage BMI = new system. Windows. Media. imaging. bitmapimage ();

 

Use Time: BMI. bgeininit ();

BMI. urisource = new uri (picname );

BMI. cacheoption = bitmapcacheoption. onload;

BMI. endinit ();

Binding: This. image. Source = BMI;

 

C # Call the local 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.