Getting Started with WPF WebCam
This tutorial will walk you through the creation of a sample application that contains RadWebCam
.
Assembly References
In order to use RadWebCam
, you will need to add references to the following assemblies:
- Telerik.Windows.Controls
- Telerik.Windows.Controls.Media
- Telerik.Windows.Controls.Navigation
- Telerik.Windows.MediaFoundation
- MediaFoundation: This dll is located in the UI for WPF installation folder/Binaries or Binaries.NoXaml/{.NET Version}/MediaFoundation folder.
- SharpDX: This dll is located in the UI for WPF installation folder/Binaries or Binaries.NoXaml/{.NET Version}/SharpDX folder.
- SharpDX.Direct3D9: This dll is located in the UI for WPF installation folder/Binaries or Binaries.NoXaml/{.NET Version}/SharpDX folder.
You can find the required assemblies for each control from the suite in the Controls Dependencies help article.
RadWebCam uses Microsoft Media Foundation which requires a minimum OS version of Windows Vista or later. Also, some versions of Windows 7 don't have the Media Feature Package installed, so you may need to install it separately.
Setting up the Control
To start using the control you only need to add it in the visual tree through XAML or code-behind. This will automatically detect a web camera if one is connected to the device and start receiving the media stream, thus showing the camera input.
Defining RadWebCam in XAML
<telerik:RadWebCam />
Defining RadWebCam in code
RadWebCam radWebCam = new RadWebCam();
RadWebCam
Auto Start
By default the camera control will start automatically if a camera device is connected. You can change this by setting the AutoStart
property of RadWebCam to False
.
Setting AutoStart in XAML
<telerik:RadWebCam AutoStart="False"/>
Setting AutoStart in code
this.radWebCam.AutoStart = false;
Connect to a Camera Manually
To start the webcam with the default settings you can call the Start
method of the control. Additionally, you can start a specific camera device and choose a different video format and microphone by calling the Initialize
method. Read more about this in the Start the Camera article.
Stop the Camera
To stop the stream between the camera device and the RadWebCam control, call the Stop
method.
Stopping the camera
this.radWebCam.Stop();
Recording Video
To record a video you need to set RecordingFilePath
before start the recording. This is required in order to specify where the recording will be stored on the file system.
To start recording, press the "Start recording" button or call the StartRecording
method of RadWebCam. This will start writing the media stream to the corresponding file.
Set the recording file path in XAML
<telerik:RadWebCam RecordingFilePath="C:\temp\video.mp4"/>
Read more about this in the Recording Video article.
Taking Snapshot
A snapshot of the currently displayed video feed can be taken using the TakeSnapshot
method of the control, or by pressing the "Take snapshot button". This will fire the SnapshotTaken
event where you get access the current snapshot as a BitmapSource
object.
Subscribing to the SnapshotTaken event
public MainWindow()
{
InitializeComponent();
this.radWebCam.SnapshotTaken += RadWebCam_SnapshotTaken;
}
private void RadWebCam_SnapshotTaken(object sender, SnapshotTakenEventArgs e)
{
BitmapSource snapshot = e.Snapshot;
// here you save the source to a file, in memory, or to show it in the UI
}