In the age of VS6.0, Microsoft provided a powerful and convenient Library such as VFW (Video For Window) to support Video capture and playback. But whether in.NETThe v1.0 or. NET v2.0 framework does not provide corresponding class libraries to support video capture and playback. There are many ways to solve this problem. For example, you can use the platform to call P/Invoke to block the functional functions in VFW and then call them in C. This is very inefficient and complicated. We can also use ActiveX controls provided by third parties to implement this function. What are the advantages of this function? Simple !!!! This is enough. Third-party controls are often more powerful. But there is no free lunch in the world-such controls are often charged. Some may think: "Isn't it still possible to use DX (DirectX) to implement it? Microsoft also provides the Manager DX-managed code SDK ". Indeed, using DX to achieve video capture and playback is a good method, both in terms of efficiency and effect. Unfortunately, although Microsoft has provided D3D, DirectDraw, DirectSound, and DirectPlayer... but we are not interested in it, we can implement the DirectShow SDK for video capture. As far as I know, there are open-source code in foreign countries that encapsulate DirectShow. I have also used it. I personally feel pretty good. If you are interested, you can find this information, I will not talk about it here.
To achieve video capture in C # using ActiveX, make the following preparations:
First of all, we need to have a development environment of VS2003 or VS2005, which I will not say much. I believe everyone on Earth knows.
The second is to install the camera driver, that is, you can see your own camera images during QQ or MSN video chat.
Then it is the most important: Install ActiveX controls that support video capture! What controls? Pegasus CapturePro. Where to download? : The http://www.cncode.com/downinfo/3504.html address should be downloadable. I just tested it again. But I cannot guarantee it will always work. This control is charged. There is an evaluation version on the Internet. I believe there is a way for everyone to do it. If it doesn't work, send me an Email and I will tell you what to do. After "Next" is installed, you can.
Create a new"
WindowsAfter the project is created, add ActiveX controls to the toolbox by clicking Add/Remove toolbox items in the Tools menu of VS2003 ". In the displayed dialog box, select the "COM components" tab and select the check box before "Pegasus Imaging CapturePRO Control v3.0" in the list box below, click OK to return to the compiler editing page. Drag and Drop the added widget to the form to adjust the size and position, and modify the attributes on the properties page. The method for modifying attributes is the same as that for common controls. For the meaning of each attribute, refer to the help document of the control. All the methods, properties, and events of the control are described in the help document. We recommend that you modify the "Name" attribute to facilitate future operations. For example, you can change the "Name" attribute to axCap (the control will be indicated by axCap later ). Change the Size attribute of axCap to 320,240. Because the default resolution of many cameras is 320X240. The layout of the form is roughly as follows:
Then, place a Button control on the form and add the following code in the Click Event of the control:
View plaincopy to clipboardprint?
Private void butConnect_Click (object sender, System. EventArgs e)
...{
AxCap. Connect (0); // Connect to the device
AxCap. Preview = true; // start Preview
}
Private void butConnect_Click (object sender, System. EventArgs e)
...{
AxCap. Connect (0); // Connect to the device
AxCap. Preview = true; // start Preview
}
Then compile the project, allow the program, and click the button to view the video image.
Here, the operation is successful and the most basic functions are implemented. Here, we will briefly explain the above two lines of code. The first line of axCap. Connect (0) indicates that the control is connected to device 0. In Windows, multiple video devices are supported at the same time. Each device has a number. The first device number is 0, the second device number is 1, and so on. Because only one video camera is connected to my computer, the video device number is 0. Here we can see that the parameters of the Connect () function are actually the parameters of the Video device. For how to determine the number of the Video device, the following will be a detailed description. The second line axCap. Preview = true; indicates opening the Preview. When the Preview attribute is true, it indicates that the video image is displayed on the control. Of course, if this attribute is false, we will not see the video.
The following describes some common and important methods and attributes:
How to determine the video device number: first, use the attribute axCap. NumDevices to obtain the number of available video devices in Windows, and then use the method axCap. ObtainDeviceName (I) to obtain the name string of the device. The parameter I is the same as the Connect () function, indicating the video device number. For specific methods and implementation processes, refer to the following code:
View plaincopy to clipboardprint?
// Identifies useless Capture Devices.
If (axCap. NumDevices <0)
{
// If no available video device is found, an error is reported and the program is exited.
MessageBox. Show ("no available devices found! "," Error ", MessageBoxButtons. OK, MessageBoxIcon. Error );
CobDevice. Items. Add ("available video devices not found ");
CobDevice. SelectedIndex = 0;
}
Else
{
// If it is found, the device is enumerated and the device combo box is initialized.
Try
{
For (int I = 0; I <axCap. NumDevices; I ++)
{
CobDevice. Items. Add (axCap. ObtainDeviceName (I ));
}
CobDevice. SelectedIndex = 0; // sets the default device.
}
Catch
{
MessageBox. Show ("device initialization failed! "," Error ", MessageBoxButtons. OK, MessageBoxIcon. Error );
CobDevice. Items. Add ("available video devices not found ");
CobDevice. SelectedIndex = 0;
}
}
// Identifies useless Capture Devices.
If (axCap. NumDevices <0)
{
// If no available video device is found, an error is reported and the program is exited.
MessageBox. Show ("no available devices found! "," Error ", MessageBoxButtons. OK, MessageBoxIcon. Error );
CobDevice. Items. Add ("available video devices not found ");
CobDevice. SelectedIndex = 0;
}
Else
{
// If it is found, the device is enumerated and the device combo box is initialized.
Try
{
For (int I = 0; I <axCap. NumDevices; I ++)
{
CobDevice. Items. Add (axCap. ObtainDeviceName (I ));
}
CobDevice. SelectedIndex = 0; // sets the default device.
}
Catch
{
MessageBox. Show ("device initialization failed! "," Error ", MessageBoxButtons. OK, MessageBoxIcon. Error );
CobDevice. Items. Add ("available video devices not found ");
CobDevice. SelectedIndex = 0;
}
}
How to modify camera parameters: Each camera has many parameters, such as brightness, contrast, and grayscale. Some cameras may also provide many special effects, such as frames, black and white. These parameters and effects can be modified by calling the underlying video device Properties dialog box. For details, refer to the following code:
View plaincopy to clipboardprint?
Private void butSet_Click (object sender, System. EventArgs e)
{
// Call the DrawShow Setting Dialog Box
If (axCap. HasFilterPropertyPage (CAPTUREPRO3Lib. FILTERPROPTYPE. FILTERPROP_VIDEO_DEVICE ))
{
AxCap. ShowFilterPropertyPage (CAPTUREPRO3Lib. FILTERPROPTYPE. FILTERPROP_VIDEO_DEVICE ,"");
}
}
Private void butSet_Click (object sender, System. EventArgs e)
{
// Call the DrawShow Setting Dialog Box
If (axCap. HasFilterPropertyPage (CAPTUREPRO3Lib. FILTERPROPTYPE. FILTERPROP_VIDEO_DEVICE ))
{
AxCap. ShowFilterPropertyPage (CAPTUREPRO3Lib. FILTERPROPTYPE. FILTERPROP_VIDEO_DEVICE ,"");
}
}
Video Recording: you must first set the video file. You can use the axCap. StreamFile attribute to set the video file path. This attribute refers to the full path of the video file. You can set Preview = true to enable Preview. Call axCap. StartCapture () to start recording. To stop recording, you only need to call the axCap. EndCapture () function. You can refer to the following code:
View plaincopy to clipboardprint?
Private void butStartCap_Click (object sender, System. EventArgs e)
...{
If (saveFile. FileName! = "") // If the path is set, perform the operation. Otherwise, an error is returned.
...{
If (radCapFarme. Checked) // perform the frame capture operation
...{
If (! FlagCapingFarmes) // if no other capture is currently executed
...{
If (! AxCap. Preview) // Enable Automatic Preview
...{
AxCap. Visible = true;
AxCap. Preview = true;
TxtBack. Visible = false;
ButCaptureImg. Enabled = true;
ButPreview. Text = "Stop preview ";
}
If (chkAutoSave. Checked)
...{
// If it is automatically saved as true, set the cycle
AxCap. Interval = Convert. ToInt32 (txtTime. Text, 10); // set the Automatic save time first
}
AxCap. AutoSave = chkAutoSave. Checked; // process automatically saved
AxCap. AutoIncrement = chkAutoRename. Checked; // process automatic file name change
AxCap. FrameFile = saveFile. FileName; // you can specify the storage path.
ButStartCap. Text = "Stop capture ";
FlagCapingFarmes = true; // set the flag to be busy.
AxCap. CaptureFrame (); // starts to capture frames.
}
Else // if other capturing work is in progress
...{
AxCap. AutoSave = false; // disable auto save
AxCap. Interval = 0; // set the automatic storage period to 0.
ButStartCap. Text = "Start capture ";
FlagCapingFarmes = false; // set the idle flag
DisConnect (); // DisConnect to close the capture
}
}
Else // execute stream capture
...{
If (! FlagCapingStream)
...{
AxCap. VideoCompressorIndex = cobVicomp. SelectedIndex;
AxCap. AudioDeviceIndex = comAudioDevice. SelectedIndex;
AxCap. AudioCompressorIndex = comAudioComp. SelectedIndex;
AxCap. StreamFile = saveFile. FileName;
ButStartCap. Text = "Stop capture ";
FlagCapingStream = true;
If (! AxCap. Preview) // Enable Automatic Preview
...{
AxCap. Visible = true;
AxCap. Preview = true;
TxtBack. Visible = false;
ButCaptureImg. Enabled = true;
ButPreview. Text = "Stop preview ";
}
Try
...{
AxCap. StartCapture ();
}
Catch
...{
MessageBox. Show ("video encoder is not available, please connect the device again ");
CobVicomp. Items. Clear ();
ButStartCap. Text = "Start capture ";
FlagCapingStream = false;
AxCap. Preview = false;
MnuDisableLink. Enabled = false;
MnuLink. Enabled = true;
CobColor. Enabled = false;
CobPix. Enabled = false;
CobColor. Text = null;
CobColor. Items. Clear ();
CobPix. Text = null;
CobPix. Items. Clear ();
TxtBack. Visible = true;
ChkAdvCap. Enabled = false;
ButPreview. Enabled = false;
ButSet. Enabled = false;
ButDeviceSet. Enabled = false;
ButVideoFormat. Enabled = false;
ButCaptureImg. Enabled = false;
ChkData. Enabled = false;
ChkTime. Enabled = false;
TxtLable. Enabled = false;
ButPreview. Text = "Start preview ";
ButLink. Text = "connecting devices ";
If (chkAdvCap. Checked)
ChkAdvCap. CheckState = CheckState. Unchecked;
VisableControl (false, 3 );
}
}
Else
...{
If (axCap. Preview)
...{
AxCap. Preview = false;
AxCap. Visible = false;
TxtBack. Visible = true;
ButCaptureImg. Enabled = false;
ButPreview. Text = "Start preview ";
}
ButStartCap. Text = "Start capture ";
FlagCapingStream = false;
AxCap. EndCapture ();
}
}
}
Else
...{
MessageBox. Show ("set the file to be stored first! "," Error ", MessageBoxButtons. OK, MessageBoxIcon. Error );
}
}
Private void butStartCap_Click (object sender, System. EventArgs e)
...{
If (saveFile. FileName! = "") // If the path is set, perform the operation. Otherwise, an error is returned.
...{
If (radCapFarme. Checked) // perform the frame capture operation
...{
If (! FlagCapingFarmes) // if no other capture is currently executed
...{
If (! AxCap. Preview) // Enable Automatic Preview
...{
AxCap. Visible = true;
AxCap. Preview = true;
TxtBack. Visible = false;
ButCaptureImg. Enabled = true;
ButPreview. Text = "Stop preview ";
}
If (chkAutoSave. Checked)
...{
// If it is automatically saved as true, set the cycle
AxCap. Interval = Convert. ToInt32 (txtTime. Text, 10); // set the Automatic save time first
}
AxCap. AutoSave = chkAutoSave. Checked; // process automatically saved
AxCap. AutoIncrement = chkAutoRename. Checked; // process automatic file name change
AxCap. FrameFile = saveFile. FileName; // you can specify the storage path.
ButStartCap. Text = "Stop capture ";
FlagCapingFarmes = true; // set the flag to be busy.
AxCap. CaptureFrame (); // starts to capture frames.
}
Else // if other capturing work is in progress
...{
AxCap. AutoSave = false; // disable auto save
AxCap. Interval = 0; // set the automatic storage period to 0.
ButStartCap. Text = "Start capture ";
FlagCapingFarmes = false; // set the idle flag
DisConnect (); // DisConnect to close the capture
}
}
Else // execute stream capture
...{
If (! FlagCapingStream)
...{
AxCap. VideoCompressorIndex = cobVicomp. SelectedIndex;
AxCap. AudioDeviceIndex = comAudioDevice. SelectedIndex;
AxCap. AudioCompressorIndex = comAudioComp. SelectedIndex;
AxCap. StreamFile = saveFile. FileName;
ButStartCap. Text = "Stop capture ";
FlagCapingStream = true;
If (! AxCap. Preview) // Enable Automatic Preview
...{
AxCap. Visible = true;
AxCap. Preview = true;
TxtBack. Visible = false;
ButCaptureImg. Enabled = true;
ButPreview. Text = "Stop preview ";
}
Try
...{
AxCap. StartCapture ();
}
Catch
...{
MessageBox. Show ("video encoder is not available, please connect the device again ");
CobVicomp. Items. Clear ();
ButStartCap. Text = "Start capture ";
FlagCapingStream = false;
AxCap. Preview = false;
MnuDisableLink. Enabled = false;
MnuLink. Enabled = true;
CobColor. Enabled = false;
CobPix. Enabled = false;
CobColor. Text = null;
CobColor. Items. Clear ();
CobPix. Text = null;
CobPix. Items. Clear ();
TxtBack. Visible = true;
ChkAdvCap. Enabled = false;
ButPreview. Enabled = false;
ButSet. Enabled = false;
ButDeviceSet. Enabled = false;
ButVideoFormat. Enabled = false;
ButCaptureImg. Enabled = false;
ChkData. Enabled = false;
ChkTime. Enabled = false;
TxtLable. Enabled = false;
ButPreview. Text = "Start preview ";
ButLink. Text = "connecting devices ";
If (chkAdvCap. Checked)
ChkAdvCap. CheckState = CheckState. Unchecked;
VisableControl (false, 3 );
}
}
Else
...{
If (axCap. Preview)
...{
AxCap. Preview = false;
AxCap. Visible = false;
TxtBack. Visible = true;
ButCaptureImg. Enabled = false;
ButPreview. Text = "Start preview ";
}
ButStartCap. Text = "Start capture ";
FlagCapingStream = false;
AxCap. EndCapture ();
}
}
}
Else
...{
MessageBox. Show ("set the file to be stored first! "," Error ", MessageBoxButtons. OK, MessageBoxIcon. Error );
}
}
The Pegasus CapturePRO control is a very powerful control, which is not described here due to the length and time relationships, all attributes, methods, and events of the control are described in detail in its help document. You can read them carefully when using the control. I wrote a detailed Demo program, which uses the vast majority of functions. Below I will provide the software interface and source code, hoping to help you.