C # Call Aforge class Library operation camera

Source: Internet
Author: User

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
    1. Private Filterinfocollection videodevices;
    2. private Videocapturedevice VideoSource;
    3. public int selecteddeviceindex = 0;


Here is the GET device

[CSharp]View Plaincopy
  1. Public Filterinfocollection GetDevices ()
  2. {
  3. Try
  4. {
  5. //Enumerate all video input devices
  6. Videodevices = new Filterinfocollection (Filtercategory.videoinputdevice);
  7. if (Videodevices.count! = 0)
  8. {
  9. Logclass.writefile ("video device found.");
  10. return videodevices;
  11. }
  12. Else
  13. return null;
  14. }
  15. catch (Exception ex)
  16. {
  17. Logclass.writefile ("error: No video device found! Specific reason: "+ ex."  Message);
  18. return null;
  19. }
  20. }


Select the device, then connect the camera

[CSharp]View Plaincopy
  1. <p>// <summary>
  2. /// Connect video camera
  3. // </summary>
  4. /// <param name= "Deviceindex" ></param>
  5. /// <param name= "Resolutionindex" ></param>
  6. // <returns></returns>
  7. Public videocapturedevice videoconnect (int deviceindex = 0, int resolutionindex = 0)
  8. {
  9. if (videodevices.count <= 0)
  10. return null;
  11. Selecteddeviceindex = Deviceindex;
  12. VideoSource = new Videocapturedevice (Videodevices[deviceindex].  monikerstring);</p><p> return videosource;
  13. }</p>
[CSharp]View Plaincopy
  1. Capture, photo, single frame
  2. Public void Grabbitmap (string path)
  3. {
  4. if (VideoSource = = null)
  5. return;
  6. G_path = Path;
  7. Videosource.newframe + = new Newframeeventhandler (Videosource_newframe);
  8. }


[CSharp]View Plaincopy
  1. void Videosource_newframe (object sender, AForge.Video.NewFrameEventArgs EventArgs)
  2. {
  3. Bitmap bmp = (Bitmap) eventArgs.Frame.Clone ();
  4. string fullPath = path + "temp\\";
  5. if (! Directory.Exists (FullPath))
  6. Directory.CreateDirectory (FullPath);
  7. string img = FullPath + DateTime.Now.ToString ("YyyyMMdd hhmmss") + ". bmp";
  8. Bmp. Save (IMG);
[CSharp]View Plaincopy
    1. If you don't write this here, you'll be taking pictures for a while.
    2. Videosource.newframe -= new Newframeeventhandler (videosource_newframe);
    3. }


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

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.