If there is similar, it is honored, if reproduced, please specify
Recently do the project need to operate the camera, Baidu on the internet a lot of information, many are C # call window API send SendMessage, implement operation camera, but C # call window API because of the driver problem, always pop-up Video selection dialog box, so that people are very silent, See Daniel have intercepted the window message, and then simulate click OK button, which is not to compliment Ah, and some Daniel based on the API prototype rewrite, as for me is an IT small bird, and then continue to Baidu, found a aforge strong C # class library, finally took care of, The next part of my poor code to paste out, so that peers or friends need to learn to communicate,
First use the Aforge class library: http://www.aforgenet.com/
Then reference Aforge,aforge.controls (this is the control that can be added to the toolbox), Aforge.imaging,aforge.video,aforge.video.directshow;
And then directly on the code
[CSharp]View Plaincopy
- Private Filterinfocollection videodevices;
- private Videocapturedevice VideoSource;
- public int selecteddeviceindex = 0;
Here is the GET device
[CSharp]View Plaincopy
- Public Filterinfocollection GetDevices ()
- {
- Try
- {
- //Enumerate all video input devices
- Videodevices = new Filterinfocollection (Filtercategory.videoinputdevice);
- if (Videodevices.count! = 0)
- {
- Logclass.writefile ("video device found.");
- return videodevices;
- }
- Else
- return null;
- }
- catch (Exception ex)
- {
- Logclass.writefile ("error: No video device found! Specific reason: "+ ex." Message);
- return null;
- }
- }
Select the device, then connect the camera
[CSharp]View Plaincopy
- <p>// <summary>
- /// Connect video camera
- // </summary>
- /// <param name= "Deviceindex" ></param>
- /// <param name= "Resolutionindex" ></param>
- // <returns></returns>
- Public videocapturedevice videoconnect (int deviceindex = 0, int resolutionindex = 0)
- {
- if (videodevices.count <= 0)
- return null;
- Selecteddeviceindex = Deviceindex;
- VideoSource = new Videocapturedevice (Videodevices[deviceindex]. monikerstring);</p><p> return videosource;
- }</p>
[CSharp]View Plaincopy
- Capture, photo, single frame
- Public void Grabbitmap (string path)
- {
- if (VideoSource = = null)
- return;
- G_path = Path;
- Videosource.newframe + = new Newframeeventhandler (Videosource_newframe);
- }
[CSharp]View Plaincopy
- void Videosource_newframe (object sender, AForge.Video.NewFrameEventArgs EventArgs)
- {
- Bitmap bmp = (Bitmap) eventArgs.Frame.Clone ();
- string fullPath = path + "temp\\";
- if (! Directory.Exists (FullPath))
- Directory.CreateDirectory (FullPath);
- string img = FullPath + DateTime.Now.ToString ("YyyyMMdd hhmmss") + ". bmp";
- Bmp. Save (IMG);
[CSharp]View Plaincopy
- If you don't write this here, you'll be taking pictures for a while.
- Videosource.newframe -= new Newframeeventhandler (videosource_newframe);
- }
This completes the operation of the camera.
But found a problem, if you want to take pictures of the photos to be processed in the save, here is a problem, so you need to add the control in the interface foreground, medical aforge.controls, and then add to the toolbox, and then drag the Videosourceplayer control into the form, To get a single image processing:
Bitmap bmp = Videosourceplayer1.getcurrentframe ();
This can be handled, Aforge class library is very powerful, here is just the tip of the iceberg, the article is not enough to ask you a lot of points, welcome to put forward valuable comments and suggestions. Thank you...
Transferred from: http://blog.csdn.net/chenhongwu666/article/details/40594365
C # Call Aforge class Library operation camera