C # operation camera to achieve camera surveillance video function

Source: Internet
Author: User

Since the formal work has been done is based on B/s web development, has been a long time does not study C/s things, but by a friend commissioned to help him do a photo of such a small function. In fact, there are a lot of similar code online, but there are few estimates that can really be run. Originally said the weekend to do, but these two days have been very messy brain, also did not calm down, the evening, do not do tomorrow a bit embarrassed ah, can only temporarily do one. Find materials on the Internet to do their own process has also found some problems, so get a blog to share, so that there is a need for friends can be used directly, and second is the study of the encounter these problems can be solved.

Just beginning to contact the camera video and so on when the sophomore, because the lab has a small project handed to me, so at that time to play video and so on some understanding. At that time the demand is to have recorded the monitoring video to do processing, the whole process is no longer need to control their own video, so relatively simple. While taking pictures and photographing is your own control, so a little bit more complicated. Come on, let's talk about it.

Or the past style, only to achieve the function of the interface does not do too much landscaping, the need for friends can do their own hands. Compare these days to do their own, ample clothing. Figure 1-1 is the main interface of the program:

Figure 1-1

Operation of the camera and the realization of the photo function the whole process is mainly through a third party components, called Aforge, is a foreign component, so open up a bit slow, but to have patience, now has been updated to 2.2.5 version. If you do not want to download from the official web, the end of the article also gave the corresponding download address, the need to use the words directly to the OK. The procedure is also very simple, a WinForm page, add a reference to the Aforge can be, but this process will refer to some other DLLs, some are not too common, so the reference to the DLL also made a screenshot (Figure 1-2), in the process of doing their own reference can be.

Figure 1-2

The middle part of figure 1-1 above is used to display the contents of the camera when it is turned on in real time, and is a custom control. To illustrate this, drag the AForge.Controls.dll to the Toolbox area on the left, and then come out of the definition control. By the way, the custom controls we develop ourselves can also be used in this way for others. After the front desk is ready, let's start analyzing the backend code.

The whole idea is to find the camera device on the computer, then select the equipment we need to operate, and then take pictures or cameras. Today because the time relationship is only to achieve photo, the next time to do the camera function, the realization will be the same as sharing here, I hope there is a need for friends concerned.

When the form is loaded, we listen for its Load event, add the detected camera device to the rear ComboBox for the user to choose from, the key code is as follows:

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;

}

catch (ApplicationException)

{

TSCBXCAMERAS.ITEMS.ADD ("No Local capture Devices");

Videodevices = null;

}

}

When the user chooses a camera device and clicks on the connection, we turn on the camera and initialize it, key code:

Connect the camera

private void Cameraconn ()

{

Videocapturedevice VideoSource = new Videocapturedevice (videodevices[tscbxcameras.selectedindex). monikerstring);

Videosource.desiredframesize = new System.Drawing.Size (320, 240);

Videosource.desiredframerate = 1;

Videosourceplayer.videosource = VideoSource;

Videosourceplayer.start ();

}

When the user closes the click off the camera, we do the processing of the shutdown code:

Turn off the camera.

private void btnClose_Click (object sender, EventArgs e)

{

Videosourceplayer.signaltostop ();

Videosourceplayer.waitforstop ();

}

When the user clicks on the photo, we get the camera's current screen and save it to the set path, and then close the current window. Key code:

Photo

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 (Bitmapframe.create (BitmapSource));

String picname = Getimagepath () + "" + "Xiaosy" + ". jpg";

if (file.exists (Picname))

{

File.delete (Picname);

}

using (Stream stream = File.create (picname))

{

Pe.save (stream);

}

Turn off the camera when the photo is finished and refresh the form simultaneously

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 shutdown, the saved picture can be found in the personimg in the Bin directory, of course, it will be better to show the picture in the program, but because of the time relationship, it will not be added. Need friends can achieve their own, there are problems of welcome to communicate together.

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.