C # camera operations for photo taking

Source: Internet
Author: User

I have been engaged in B/S-based Web development since my official work. I have not studied C/S for a long time, but I was entrusted by my friends, this is a small function for him to take photos. In fact, there are a lot of similar code on the Internet, but there are only a few estimates that can be used for running. I used to do this on weekends, but my mind has been messy for the past two days and I have not calmed down. I am sorry if I do not do it again tomorrow. I can only do it on a temporary basis. Some problems have also been found during the process of searching for information on the Internet, so you can get a blog to share it, so that you can use it directly if you need it, 2. You can solve these problems that you are studying. When I first came into contact with cameras and videos, I was handed over to my team because of a small project in the lab. At that time, I learned a little about playing videos. At that time, the requirement was to process the recorded monitoring video. The whole process was no longer necessary to control the video, so it was relatively simple. Taking pictures and taking pictures is controlled by yourself, so it is a little complicated. Now, let's talk about it. Let's get started with the question below. In the past, only functions are implemented, and the interface is not beautified too much. If you need it, you can do it yourself. In this year, we have to work on our own. Figure 1-1 is the main interface of the program. Figure 1-1 operates the camera and the camera function. The whole process is mainly implemented through a third-party component named AForge, which is a foreign component, so it is a little slow to open, but be patient. It has been updated to version 2.2.5. If you do not want to download it from the official website, the article will also provide the corresponding content at the end of the article. If you need it, you can use it directly. The program is also very simple. On a WinForm page, you can add a reference to Aforge, but some other dll will be referenced in this process, and some are not very common, therefore, the referenced dll is also done here (Figure 1-2), and you can refer to it in your own process. Figure 1-2 the middle part of 1-1 is used to display the content obtained after the camera is turned on in real time. It is a custom control. Drag AForge. Controls. dll to the Toolbox on the left, and then customize the control. By the way, we can also use custom controls developed by ourselves in this way. After the front-end is ready, we will start to analyze the background code. The whole idea is to first find the camera device on the computer, then select the device we need to operate, and then take a photo or camera. Today, due to the time relationship, we only need to take a photo. We will share the photo with you next time, and hope you can pay attention to it. When the Form is loaded, we listen to its Load event and add the detected camera device to the ComboBox for the user to choose. The key code is as follows: copy the code private void Form1_Load (object sender, EventArgs e) {try {// enumerate all video input devices videoDevices = new FilterInfoCollection (FilterCategory. videoInputDevice); if (videoDevices. count = 0) throw new ApplicationException (); foreach (FilterInfo device in videoDevices) {tscbxCameras. items. add (device. name);} tscbxCameras. selectedIndex = 0;} c Atch (ApplicationException) {tscbxCameras. items. add ("No local capture devices"); videoDevices = null;} copy the code. When you select a camera device and click Connect, open the camera and initialize it, key code: copy the code // connect to the camera private void CameraConn () {VideoCaptureDevice videoSource = new VideoCaptureDevice (videoDevices [tscbxCameras. selectedIndex]. monikerString); videoSource. desiredFrameSize = new System. drawing. size (320,240); videoSource. desiredFrameRa Te = 1; videoSourcePlayer. videoSource = videoSource; videoSourcePlayer. start () ;}copy the code. When the user closes the camera and clicks it to close the camera, we will close it. Code: // close the camera private void btnClose_Click (object sender, EventArgs e) {videoSourcePlayer. signalToStop (); videoSourcePlayer. waitForStop ();} when the user clicks to take a photo, we get the current image of the camera, save it to the set path, and close the current window. Key code: copy the code // private void Photograph_Click (object sender, EventArgs e) {try {if (videoSourcePlayer. isRunning) {BitmapSource bitmapSource = System. windows. interop. imaging. createBitmapSourceFromHBitmap (videoSourcePlayer. getCurrentVideoFrame (). getHbitmap (), IntPtr. zero, Int32Rect. empty, BitmapSizeOptions. fromEmptyOptions (); PngBitmapEncoder pE = new PngBitmapEncoder (); pE. frames. add (BitmapFram E. create (bitmapSource); string picName = GetImagePath () + "\" + "xiaosy" + ". jpg "; if (File. exists (picName) {File. delete (picName);} using (Stream stream = File. create (picName) {pE. save (stream);} // after taking the photo, turn off the camera and refresh the form. if (videoSourcePlayer! = Null & videoSourcePlayer. isRunning) {videoSourcePlayer. signalToStop (); videoSourcePlayer. waitForStop ();} this. close () ;}} catch (Exception ex) {MessageBox. show ("camera exception:" + ex. message) ;}} private string GetImagePath () {string personImgPath = Path. getDirectoryName (AppDomain. currentDomain. baseDirectory) + Path. directorySeparatorChar. toString () + "PersonImg"; if (! Directory. exists (personImgPath) {Directory. createDirectory (personImgPath);} return personImgPath;} after the code is copied and disabled, you can find the saved image in the PersonImg in the bin directory. Of course, it will be better to display the image in the program, but it will not be added due to the time relationship. If you need it, you can implement it by yourself. If you have any questions, please contact us.

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.