New to Telerik UI for WinForms? Start a free 30-day trial
How to Deal with Slow WebCam Preview on Start
Updated over 6 months ago
Environment
| Product Version | 2021.3.1123 |
| Product | RadWebCam for WinForms |
Problem
Camera preview is very slow on its first start-up. When you click on the snapshot icon and afterwards the button with the cross to retake another snapshot, the camera preview is smooth.
Solution
One possible approach is to force taking a snapshot at least once and then reject the preview image. This can be achieved using the following code snippet:
C#
private ReadOnlyCollection<MediaFoundationDeviceInfo> videoDevices;
private ReadOnlyCollection<MediaFoundationVideoFormatInfo> videoFormats;
private ReadOnlyCollection<MediaFoundationDeviceInfo> audioDevices;
Timer t = new Timer();
public CameraView()
{
InitializeComponent();
videoDevices = RadWebCam.GetVideoCaptureDevices();
videoFormats = RadWebCam.GetVideoFormats(videoDevices[0], true);
audioDevices = RadWebCam.GetAudioCaptureDevices();
}
private void CameraView_Load(object sender, EventArgs e)
{
radWebCam1.Initialized += RadWebCam1_Initialized;
radWebCam1.Initialize(videoDevices[0], videoFormats[0], audioDevices[0]);
radWebCam1.Start();
}
private void RadWebCam1_Initialized(object sender, EventArgs e)
{
t.Interval = 200;
t.Tick += T_Tick;
t.Start();
}
private void T_Tick(object sender, EventArgs e)
{
t.Stop();
radWebCam1.TakeSnapshot();
radWebCam1.DiscardSnapshot();
}
An alternative solution if the audio is not important for the precise scenario, feel free to pass null parameter to it:
C#
radWebCam1.Initialize(videoDevices[0], videoFormats[0], null);