[C #] #100 call the camera,

Source: Internet
Author: User

[C #] #100 call the camera,

Requirement: because the project needs to get the user's profile picture, you need to use C # To call the camera to get the profile picture.

Below is a method to call the camera.

Case: call a DEMO of the camera 【]

Class library used: AForge. dll [Demo download, Demo contains class library]

1. Detect camera devices on the computer

FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

 

Use Count to determine the number of camera devices. If there is no camera, an exception will be thrown. If there is a camera, add it to the drop-down box.

if (videoDevices.Count == 0)           throw new ApplicationException(); foreach (FilterInfo device in videoDevices) {           tscbxCameras.Items.Add(device.Name); }

 

2. Connect the camera

After detecting the camera, you can connect to the camera to obtain the image.

// Select VideoCaptureDevice videoSource = new VideoCaptureDevice (videoDevices [tscbxCameras. selectedIndex]. monikerString); // you can specify the size of the videoSource image. desiredFrameSize = new Size (320,240); videoSource. desiredFrameRate = 1; // sets the videPlayer for the image obtained by the camera control. videoSource = videoSource; // enable the camera videPlayer. start ();

 

3. Disable the cameraYou can also disable it by using the Stop () method]

videPlayer.SignalToStop();videPlayer.WaitForStop();

 

 

Run the Demo:

There is one more project: draw the picture on the camera

Img = new Bitmap (102,126, PixelFormat. format24bppRgb); // sets the image size and number of digits videPlayer. drawToBitmap (Bitmap) img, new Rectangle (0, 0, videPlayer. width, videPlayer. height); // draw the image to the Img object videPicture. image = img; // displayed on the PictureBox Control

 

I encountered a problem here, that is, the common error of GDI + 【Cause: Resource usage]

Problems:

1. When modifying personnel information, read the image file locally and assign the value to PictureBox.

2. Save the PictureBox image to the local hard disk.

Solution :【Remove image resource usage]

1. Read the local image and copy it in depth.

2. Assign the copy of the deep copy to PictureBox.

3. Disable local image resources

# Region: Copies images in depth and disables resources to prevent the use of images in depth. In addition, img = new Bitmap (path) is disabled ); image bmp = new Bitmap (img. width, img. height); Graphics draw = Graphics. fromImage (bmp); draw. drawImage (img, 0, 0); draw. dispose (); img. dispose (); videPicture. image = bmp; # endregion

Project:

Related Article

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.