[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: