In our previous project, do the picture capture of the camera, we generally still need to do a package processing, in the newer version of the DevExpress control, added a Cameracontrol control, you can directly call the camera display, so you can also do avatar capture functions, This article describes how to do the relevant image acquisition operation based on this control.
1, Cameracontrol Control introduction
The control starts with 15.1 and joins the control group, and the control is easy to use and can be used on the WinForm interface by dragging directly to the interface. This case is developed based on 16.1 and therefore also has these control modules.
For example, we can directly drag the Cameracontrol control to the interface, and then run the interface to see the following effect.
In the camera interface, there is a configuration option, you can set the resolution of the camera and other parameters for processing.
On the Pictureedit control, you can also turn on the camera capture function, just set it in the control properties:
The operating interface is shown below.
This collection of pictures of the menu item text is not Chinese, so a bit strange, we can also use its built-in dialog box (takepicturedialog ) for image capture operations.
The specific code looks like this:
Private void Btninternal_click (object sender, EventArgs e) { new Takepicturedialog (); if (D.showdialog () = = System.Windows.Forms.DialogResult.OK) { this. pictureedit1.image = d.image; }}
The resulting dialog box is built-in and the interface cannot be modified, including text content.
2. Custom Capture dialog box
In order to better realize the acquisition of Avatar, we can imitate this dialog module, do a custom form, to achieve the camera image capture operation, as shown below.
At the same time we set a public image object property in the dialog form, as shown below
/// <summary> /// Collect pictures /// </summary> Public Get set; }
This makes it easy to set up and extract image information.
The code to capture the image is to get a snapshot from the camera control, as shown below.
Private void Btncapture_click (object sender, EventArgs e) { this this . Cameracontrol1.takesnapshot (); This This . cameraimage; }
Then, when the form exits, remember to release the resources of the camera, or the second time will be prompted to occupy, can not be used.
Private void Frmcamera_formclosing (object sender, FormClosingEventArgs e) { this. Cameracontrol1.dispose (); }
The complete code for the entire dialog box is shown below.
Public Partial classFrmcamera:form {/// <summary> ///Collect pictures/// </summary> PublicImage Cameraimage {Get;Set; } PublicFrmcamera () {InitializeComponent (); } Private voidBtnok_click (Objectsender, EventArgs e) { This. DialogResult =System.Windows.Forms.DialogResult.OK; } Private voidBtncapture_click (Objectsender, EventArgs e) { This. Cameraimage = This. Cameracontrol1.takesnapshot (); This. Pictureedit1.image = This. Cameraimage; } Private voidFrmcamera_formclosing (Objectsender, FormClosingEventArgs e) { This. Cameracontrol1.dispose (); } }
The last run can get the interface as shown below.
This kind of processing is better, I am in the base Interface class library before the control comes out, using the portrait control to handle the camera image collection, as shown in my blog introduction to the WinForm development framework of the portrait display save control implementation.
For example, in the member management system, is the use of portrait management control, through the previous camera common class library implementation of the graphics acquisition operations.
This is actually done using a camera class library, but now that DevExpress itself provides control support, we can also use it directly, so it's more convenient and less code.
Camera image acquisition using the Cameracontrol control in DevExpress