SlideShare a Scribd company logo
REC 
How to implement 
CAMERA RECORDING 
for USB WEBCAM or IP CAMERA in C#.NET 
! 
SOURCE CODE: 
Welcome to this presentation that explains step-by-step how to develop video 
recording feature for your USB webcam and your IP camera / ONVIF IP camera in 
C#.NET to be able to capture and save the camera image. Good luck, have fun! 
www.camera-sdk.com
Contents 
Prerequisites 
Creating a WPF project in Visual Studio 
Building a camera viewer 
Implementing the video recorder feature 
Testing the application 
! 
SOURCE C2 O/ 1D7 E: 
www.camera-sdk.com
Prerequisites 
Windows PC 
Broadband Internet connection 
USB webcam or IP camera connected to your network 
Microsoft Visual Studio + .NET Framework 4.0 
OZEKI Camera SDK 
Note: Make sure that you have stable Internet connection, and your PC and the camera is connected 
to the same network. If it is needed, download and install the IDE and the .NET Framework from 
www.microsoft.comand the camera SDK from www.camera-sdk.com, too. 
3 / 17
Creating a WPF app in VS 
Creating a new project 
• Click on the „File” and choose the „New Project…” option 
• Choose the Visual C# WPF Application 
• Provide a name for your project and click on the „OK” 
Add the Camera SDK into the References 
• Right-click on the „References” 
• Click on the „Add Reference…” 
• Select the „Browse” tab and look for the „VoIPSDK.dll” 
• Select the .dll file and click on the „OK” 
4 / 17
Building a camera viewer 
Creating the GUI in the xaml file 
Create 2 buttons for USB camera connection and disconnection: 
<GroupBox Header="Connect to USB camera" Height="100" Width="150„ HorizontalAlignment="Left" 
VerticalAlignment="Top"> 
<Grid> 
<Button Content="Connect" Width="75" Margin="32,19,0,0" 
HorizontalAlignment="Left" VerticalAlignment="Top" 
Click="ConnectUSBCamera_Click"/> 
<Button Content="Disconnect" Width="75" Margin="32,46,0,0" 
HorizontalAlignment="Left" VerticalAlignment="Top" 
Click="DisconnectUSBCamera_Click"/> 
</Grid> 
</GroupBox> 
5 / 17
Building a camera viewer 
Creating the GUI in the xaml file 
Create 2 buttons and 3 textboxes (IP address, username, password) for IP cam connection/disconnection: 
<GroupBox Header="Connect to IP camera" Height="100" Width="387" HorizontalAlignment="Left" 
VerticalAlignment="Top" Margin="155,0,0,0"> 
<Grid> 
<Label Height="25" Width="70" Content="Host" HorizontalAlignment="Left" VerticalAlignment="Top"/> 
<TextBox Name="HostTextBox" HorizontalAlignment="Left" VerticalAlignment="Top" Height="23" 
Width="169" Margin="68,2,0,0" TextWrapping="Wrap" /> 
<Label Height="25" Width="70" Content="Username" HorizontalAlignment="Left" Margin="0,26,0,0" 
VerticalAlignment="Top"/> 
<TextBox Name="UserTextBox" HorizontalAlignment="Left" VerticalAlignment="Top" Height="23" 
Width="169" Margin="68,29,0,0" TextWrapping="Wrap"/> 
<Label Height="25" Width="70" Content="Password" HorizontalAlignment="Left" VerticalAlignment="Top" 
Margin="0,52,0,0" /> 
<PasswordBox Name="Password" HorizontalAlignment="Left" VerticalAlignment="Top" Height="25" 
Width="169" Margin="68,56,0,0"/> 
<Button Content="Connect" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="267,16,0,0" 
Width="75" Click="ConnectIPCamera_Click"/> 
<Button Content="Disconnect" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="267,43,0,0" 
Width="75" Click="DisconnectIPCamera_Click" /> 
</Grid> 
</GroupBox> 
6 / 17
Building a camera viewer 
Creating the GUI in the xaml file 
After you have created the GUI elements that allow the user to be able to connect to a USB or an 
IP camera, you need to build a „camera box” to be able to display the camera image in the GUI: 
<Grid Name="CameraBox" Margin="10,105,10,166"/> 
Set the window property as follows: 
ResizeMode="NoResize" WindowStartupLocation="CenterScreen"> 
7 / 17
Building a camera viewer 
Building the image displaying feature in the xaml.cs file 
In the xaml.cs file, first of all, you need to add some extra using lines. All the essential namespaces 
are determined by the camera software: 
using Ozeki.Media.IPCamera; 
using Ozeki.Media.MediaHandlers; 
using Ozeki.Media.MediaHandlers.Video; 
using Ozeki.Media.Video.Controls; 
After this, you need to add some objects that are needed for displaying the camera image: 
private VideoViewerWPF _videoViewerWpf; 
private BitmapSourceProvider _provider; 
private IIPCamera _ipCamera; 
private WebCamera _webCamera; 
private MediaConnector _connector; 
8 / 17
Building a camera viewer 
Building the image displaying feature in the xaml.cs file 
Instantiate the objects in the constructor and call the SetVideoViewer() helpfunction: 
public MainWindow() 
{ 
InitializeComponent(); 
_connector = new MediaConnector(); 
_provider = new BitmapSourceProvider(); 
SetVideoViewer(); 
} 
Create the SetVideoViewer() helpfunction that creates and sets the videoviewer object and add it to the GUI: 
private void SetVideoViewer() 
{ 
_videoViewerWpf = new VideoViewerWPF 
{ 
HorizontalAlignment = HorizontalAlignment.Stretch, 
VerticalAlignment = VerticalAlignment.Stretch, 
Background = Brushes.Black 
}; 
CameraBox.Children.Add(_videoViewerWpf); 
_videoViewerWpf.SetImageProvider(_provider); 
} 
9 / 17
Building a camera viewer 
Building the image displaying feature in the xaml.cs file 
Implement the USB camera connector and disconnector: 
private void ConnectUSBCamera_Click(object sender, RoutedEventArgs e) 
{ 
_webCamera = WebCamera.GetDefaultDevice(); 
if (_webCamera == null) return; 
_connector.Connect(_webCamera, _provider); 
_webCamera.Start(); 
_videoViewerWpf.Start(); 
} 
private void DisconnectUSBCamera_Click(object sender, RoutedEventArgs e) 
{ 
_videoViewerWpf.Stop(); 
_webCamera.Stop(); 
_webCamera.Dispose(); 
_connector.Disconnect(_webCamera, _provider); 
} 
10 / 17
Building a camera viewer 
Building the image displaying feature in the xaml.cs file 
Implement the IP camera connector/disconnector (including the required network parameters as well): 
private void ConnectIPCamera_Click(object sender, RoutedEventArgs e) 
{ 
var host = HostTextBox.Text; 
var user = UserTextBox.Text; 
var pass = Password.Password; 
_ipCamera = IPCameraFactory.GetCamera(host, user, pass); 
if (_ipCamera == null) return; 
_connector.Connect(_ipCamera.VideoChannel, _provider); 
_ipCamera.Start(); 
_videoViewerWpf.Start(); 
} 
private void DisconnectIPCamera_Click(object sender, RoutedEventArgs e) 
{ 
_videoViewerWpf.Stop(); 
_ipCamera.Disconnect(); 
_ipCamera.Dispose(); 
_connector.Disconnect(_ipCamera.VideoChannel, _provider); 
} 
11 / 17
Implementing the recording 
Extending the GUI in the xaml file 
Create 2 buttons that will start and stop the video capturing (after you have added the additional 
elements, you need to generate 2 eventhandler methods for them): 
<GroupBox Header="Function" Height="160" Width="542" Margin="0,360,0,0" 
HorizontalAlignment="Left" VerticalAlignment="Top" > 
<Grid> 
<Grid.ColumnDefinitions> 
<ColumnDefinition Width="*"/> 
<ColumnDefinition Width="*"/> 
</Grid.ColumnDefinitions> 
<Button Grid.Column="0" Content="Start video capture" 
HorizontalAlignment="Center" VerticalAlignment="Center" 
Click="StartCapture_Click" Width="120" Height="50"/> 
<Button Grid.Column="1" Content="Stop video capture" 
HorizontalAlignment="Center" VerticalAlignment="Center" 
Click="StopCapture_Click" Width="120" Height="50"/> 
</Grid> 
</GroupBox> 
12 / 17
Implementing the recording 
Building the video recorder in the xaml.cs file 
Declare 2 new fields for video recording. 
These two objects are the followings: IVideoSender and MPEG4Recorder: 
private MPEG4Recorder _recorder; 
private IVideoSender _videoSender; 
You need to instantiate them at USB webcam connection and IP camera connection as well by 
inserting both of the following lines: 
_videoSender = _webCamera; 
_videoSender = _ipCamera.VideoChannel; 
Into the USB camera connection section: 
Into the IP camera connection section: 
13 / 17
Implementing the recording 
Building the video recorder in the xaml.cs file 
To differentiate the captured streams, their file name will contain the current date and time. The 
destination folder will be the place of the program. Instantiate the recorder and subscribe the 
eventhandler method for the MultiplexFinished event. Connect the videosender and the recorder: 
private void StartCapture_Click(object sender, RoutedEventArgs e) 
{ 
if (_videoSender == null) return; 
var date = DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + 
DateTime.Now.Hour + "-" + DateTime.Now.Minute + "-" + 
DateTime.Now.Second; 
var currentpath = AppDomain.CurrentDomain.BaseDirectory + date + ".mpeg4"; 
_recorder = new MPEG4Recorder(currentpath); 
_recorder.MultiplexFinished += _recorder_MultiplexFinished; 
_connector.Connect(_videoSender, _recorder.VideoRecorder); 
} 
14 / 17
Implementing the recording 
Building the video recorder in the xaml.cs file 
Create the eventhandler method. Unsubscribe from the event and dispose the recorder. To be able 
to stop capturing, you need to disconnect and call the Multiplex method that creates the video. 
void _recorder_MultiplexFinished(object sender, Ozeki.VoIP.VoIPEventArgs<bool> e) 
{ 
_recorder.MultiplexFinished -= _recorder_MultiplexFinished; 
_recorder.Dispose(); 
} 
private void StopCapture_Click(object sender, RoutedEventArgs e) 
{ 
if (_videoSender == null) return; 
_connector.Disconnect(_videoSender, _recorder.VideoRecorder); 
_recorder.Multiplex(); 
} 
15 / 17
Testing the application 
Run the application and test the video recorder 
Step 1: Connect to a camera 
• USB webcam: Click on „Connect” 
• IP camera: Enter the IP address of the camera, its 
username and password, then click on „Connect” 
Step 2: Start capturing 
Click on „Start video capture” 
Step 3: Finish capturing and save the video file 
Click on „Stop video capture” 
Step 4: Watch the video file 
16 / 17
Thank you for your attention! 
To sum it up, the ONVIF-compliant OZEKI Camera SDK provides great background 
support for your IP camera, ONVIF IP camera and USB webcam developments by 
offering prewritten components to your development environment. Building a 
video recorder to capture the camera stream is easy as that. 
For more information please visit our website: 
www.camera-sdk.com 
or send us an e-mail to: 
info@camera-sdk.com 
! 
SOURCE CODE: 
www.camera-sdk.com

More Related Content

PPT
Biology of Alternaria spp
PPTX
Fusaric acid a potent vivotoxin.pptx
PPTX
Plant virology
PPTX
Molecular virology techniques.pptx
PDF
Mycoviruses of filamentous fungi and their relevance to plant pathology
PDF
Chemical composition and physical properties of plant viruses
PPT
Apple diseases by Nazia Manzar
PPTX
Araceae family
Biology of Alternaria spp
Fusaric acid a potent vivotoxin.pptx
Plant virology
Molecular virology techniques.pptx
Mycoviruses of filamentous fungi and their relevance to plant pathology
Chemical composition and physical properties of plant viruses
Apple diseases by Nazia Manzar
Araceae family

What's hot (20)

PDF
Virus transmission and dissemination in plants Sarah Ashfaq 51.pdf
PPT
JAVA Servlets
PPTX
Transmission of plant viruses
PPTX
Movement of viruses
PPTX
Plant Pathology
PDF
Fungi
PPTX
nematode management in organic agriculture.pptx
PDF
Arquitetura Node com NestJS
PPTX
trichoderma.pptx
PPTX
Virus and virod disease
PPTX
Diseases of field crops and etiology
DOC
Biological control
PPTX
Les 5 risques les plus critiques des applications Web selon l'OWASP
PPT
Plant viruses
PDF
Apple scab lecture final 2018
PPT
Green ear disease of bajara
PDF
History of Plant virology
PDF
Meloidogyne javanica
PPTX
Plant Viruses Diseases and Symptoms
PPTX
Plant viruses
Virus transmission and dissemination in plants Sarah Ashfaq 51.pdf
JAVA Servlets
Transmission of plant viruses
Movement of viruses
Plant Pathology
Fungi
nematode management in organic agriculture.pptx
Arquitetura Node com NestJS
trichoderma.pptx
Virus and virod disease
Diseases of field crops and etiology
Biological control
Les 5 risques les plus critiques des applications Web selon l'OWASP
Plant viruses
Apple scab lecture final 2018
Green ear disease of bajara
History of Plant virology
Meloidogyne javanica
Plant Viruses Diseases and Symptoms
Plant viruses
Ad

Viewers also liked (20)

PPT
Regulating Crowdfunding Key Considerations - Tim Wright Deep Impact India Con...
PPTX
C#.net
DOC
Top 9 c#.net interview questions answers
PPTX
MVC 6 Introduction
PPTX
ASP.NET Brief History
PPTX
Dotnet Basics Presentation
PDF
212 t5dlv gabriela soto olea francisco cuamea lizárraga_serie ohuira completa
PDF
Digital Portfolio - David Hronek, LEED AP
DOC
Mark scheme for the geographical enquiry
PDF
Bloomberg Market Concepts (BMC)
PDF
Neurological Conditions_Mediaplanet_Final_2014
DOCX
Santiago Martin
PDF
G.o. 41.073
PDF
Sintesis informativa 25 06 2013
PDF
Mnu Annual Report Draft V8
PDF
Paychex Fiscal 2010 Annual Report
PDF
A day in the life of a pharmaceutical physician
PDF
Khawvel Sunday School Ni 2014-Puitling Sunday School, Rev. Dr. Zairema Chanchin
PDF
1 St Qtr. 2008 Pri Retail Analytics
Regulating Crowdfunding Key Considerations - Tim Wright Deep Impact India Con...
C#.net
Top 9 c#.net interview questions answers
MVC 6 Introduction
ASP.NET Brief History
Dotnet Basics Presentation
212 t5dlv gabriela soto olea francisco cuamea lizárraga_serie ohuira completa
Digital Portfolio - David Hronek, LEED AP
Mark scheme for the geographical enquiry
Bloomberg Market Concepts (BMC)
Neurological Conditions_Mediaplanet_Final_2014
Santiago Martin
G.o. 41.073
Sintesis informativa 25 06 2013
Mnu Annual Report Draft V8
Paychex Fiscal 2010 Annual Report
A day in the life of a pharmaceutical physician
Khawvel Sunday School Ni 2014-Puitling Sunday School, Rev. Dr. Zairema Chanchin
1 St Qtr. 2008 Pri Retail Analytics
Ad

Similar to How to implement camera recording for USB webcam or IP camera in C#.NET (20)

PPTX
17 camera, media, and audio in windows phone 8.1
PDF
06.Programming Media on Windows Phone
PPTX
A Microsoft Silverlight User Group Starter Kit Made Available for Everyone to...
PPTX
Silverlight in Internet Scenarios
PPTX
WinRT Holy COw
PPTX
What is OZEKI Camera SDK?
PPTX
Presentation - Windows App Development - II - Mr. Chandan Gupta
PDF
Building a Native Camera Access Library - Part III - Transcript.pdf
PPT
Android cameraoverview
PPT
Ip Cam
PPTX
From Flash to Silverlight: A Rosetta Stone
PPTX
Windows 8 store apps development
KEY
openFrameworks 007 - video
PDF
Flash Prototyping Workbook - Part 1 and 2
PPTX
How to develop a WP7 app?
PPTX
Shape12 6
PPT
WPF Windows Presentation Foundation A detailed overview Version1.2
PDF
Interactive Buttons (Flash Report1)
PDF
Flash tutorials loading external images
PDF
Building a Native Camera Access Library - Part V - Transcript.pdf
17 camera, media, and audio in windows phone 8.1
06.Programming Media on Windows Phone
A Microsoft Silverlight User Group Starter Kit Made Available for Everyone to...
Silverlight in Internet Scenarios
WinRT Holy COw
What is OZEKI Camera SDK?
Presentation - Windows App Development - II - Mr. Chandan Gupta
Building a Native Camera Access Library - Part III - Transcript.pdf
Android cameraoverview
Ip Cam
From Flash to Silverlight: A Rosetta Stone
Windows 8 store apps development
openFrameworks 007 - video
Flash Prototyping Workbook - Part 1 and 2
How to develop a WP7 app?
Shape12 6
WPF Windows Presentation Foundation A detailed overview Version1.2
Interactive Buttons (Flash Report1)
Flash tutorials loading external images
Building a Native Camera Access Library - Part V - Transcript.pdf

Recently uploaded (20)

PDF
Getting Started with Data Integration: FME Form 101
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
TLE Review Electricity (Electricity).pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Tartificialntelligence_presentation.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Encapsulation theory and applications.pdf
PDF
August Patch Tuesday
PPTX
OMC Textile Division Presentation 2021.pptx
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Approach and Philosophy of On baking technology
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
Getting Started with Data Integration: FME Form 101
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Encapsulation_ Review paper, used for researhc scholars
Diabetes mellitus diagnosis method based random forest with bat algorithm
TLE Review Electricity (Electricity).pptx
Programs and apps: productivity, graphics, security and other tools
Unlocking AI with Model Context Protocol (MCP)
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Tartificialntelligence_presentation.pptx
Network Security Unit 5.pdf for BCA BBA.
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Encapsulation theory and applications.pdf
August Patch Tuesday
OMC Textile Division Presentation 2021.pptx
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Approach and Philosophy of On baking technology
Group 1 Presentation -Planning and Decision Making .pptx

How to implement camera recording for USB webcam or IP camera in C#.NET

  • 1. REC How to implement CAMERA RECORDING for USB WEBCAM or IP CAMERA in C#.NET ! SOURCE CODE: Welcome to this presentation that explains step-by-step how to develop video recording feature for your USB webcam and your IP camera / ONVIF IP camera in C#.NET to be able to capture and save the camera image. Good luck, have fun! www.camera-sdk.com
  • 2. Contents Prerequisites Creating a WPF project in Visual Studio Building a camera viewer Implementing the video recorder feature Testing the application ! SOURCE C2 O/ 1D7 E: www.camera-sdk.com
  • 3. Prerequisites Windows PC Broadband Internet connection USB webcam or IP camera connected to your network Microsoft Visual Studio + .NET Framework 4.0 OZEKI Camera SDK Note: Make sure that you have stable Internet connection, and your PC and the camera is connected to the same network. If it is needed, download and install the IDE and the .NET Framework from www.microsoft.comand the camera SDK from www.camera-sdk.com, too. 3 / 17
  • 4. Creating a WPF app in VS Creating a new project • Click on the „File” and choose the „New Project…” option • Choose the Visual C# WPF Application • Provide a name for your project and click on the „OK” Add the Camera SDK into the References • Right-click on the „References” • Click on the „Add Reference…” • Select the „Browse” tab and look for the „VoIPSDK.dll” • Select the .dll file and click on the „OK” 4 / 17
  • 5. Building a camera viewer Creating the GUI in the xaml file Create 2 buttons for USB camera connection and disconnection: <GroupBox Header="Connect to USB camera" Height="100" Width="150„ HorizontalAlignment="Left" VerticalAlignment="Top"> <Grid> <Button Content="Connect" Width="75" Margin="32,19,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Click="ConnectUSBCamera_Click"/> <Button Content="Disconnect" Width="75" Margin="32,46,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Click="DisconnectUSBCamera_Click"/> </Grid> </GroupBox> 5 / 17
  • 6. Building a camera viewer Creating the GUI in the xaml file Create 2 buttons and 3 textboxes (IP address, username, password) for IP cam connection/disconnection: <GroupBox Header="Connect to IP camera" Height="100" Width="387" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="155,0,0,0"> <Grid> <Label Height="25" Width="70" Content="Host" HorizontalAlignment="Left" VerticalAlignment="Top"/> <TextBox Name="HostTextBox" HorizontalAlignment="Left" VerticalAlignment="Top" Height="23" Width="169" Margin="68,2,0,0" TextWrapping="Wrap" /> <Label Height="25" Width="70" Content="Username" HorizontalAlignment="Left" Margin="0,26,0,0" VerticalAlignment="Top"/> <TextBox Name="UserTextBox" HorizontalAlignment="Left" VerticalAlignment="Top" Height="23" Width="169" Margin="68,29,0,0" TextWrapping="Wrap"/> <Label Height="25" Width="70" Content="Password" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,52,0,0" /> <PasswordBox Name="Password" HorizontalAlignment="Left" VerticalAlignment="Top" Height="25" Width="169" Margin="68,56,0,0"/> <Button Content="Connect" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="267,16,0,0" Width="75" Click="ConnectIPCamera_Click"/> <Button Content="Disconnect" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="267,43,0,0" Width="75" Click="DisconnectIPCamera_Click" /> </Grid> </GroupBox> 6 / 17
  • 7. Building a camera viewer Creating the GUI in the xaml file After you have created the GUI elements that allow the user to be able to connect to a USB or an IP camera, you need to build a „camera box” to be able to display the camera image in the GUI: <Grid Name="CameraBox" Margin="10,105,10,166"/> Set the window property as follows: ResizeMode="NoResize" WindowStartupLocation="CenterScreen"> 7 / 17
  • 8. Building a camera viewer Building the image displaying feature in the xaml.cs file In the xaml.cs file, first of all, you need to add some extra using lines. All the essential namespaces are determined by the camera software: using Ozeki.Media.IPCamera; using Ozeki.Media.MediaHandlers; using Ozeki.Media.MediaHandlers.Video; using Ozeki.Media.Video.Controls; After this, you need to add some objects that are needed for displaying the camera image: private VideoViewerWPF _videoViewerWpf; private BitmapSourceProvider _provider; private IIPCamera _ipCamera; private WebCamera _webCamera; private MediaConnector _connector; 8 / 17
  • 9. Building a camera viewer Building the image displaying feature in the xaml.cs file Instantiate the objects in the constructor and call the SetVideoViewer() helpfunction: public MainWindow() { InitializeComponent(); _connector = new MediaConnector(); _provider = new BitmapSourceProvider(); SetVideoViewer(); } Create the SetVideoViewer() helpfunction that creates and sets the videoviewer object and add it to the GUI: private void SetVideoViewer() { _videoViewerWpf = new VideoViewerWPF { HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch, Background = Brushes.Black }; CameraBox.Children.Add(_videoViewerWpf); _videoViewerWpf.SetImageProvider(_provider); } 9 / 17
  • 10. Building a camera viewer Building the image displaying feature in the xaml.cs file Implement the USB camera connector and disconnector: private void ConnectUSBCamera_Click(object sender, RoutedEventArgs e) { _webCamera = WebCamera.GetDefaultDevice(); if (_webCamera == null) return; _connector.Connect(_webCamera, _provider); _webCamera.Start(); _videoViewerWpf.Start(); } private void DisconnectUSBCamera_Click(object sender, RoutedEventArgs e) { _videoViewerWpf.Stop(); _webCamera.Stop(); _webCamera.Dispose(); _connector.Disconnect(_webCamera, _provider); } 10 / 17
  • 11. Building a camera viewer Building the image displaying feature in the xaml.cs file Implement the IP camera connector/disconnector (including the required network parameters as well): private void ConnectIPCamera_Click(object sender, RoutedEventArgs e) { var host = HostTextBox.Text; var user = UserTextBox.Text; var pass = Password.Password; _ipCamera = IPCameraFactory.GetCamera(host, user, pass); if (_ipCamera == null) return; _connector.Connect(_ipCamera.VideoChannel, _provider); _ipCamera.Start(); _videoViewerWpf.Start(); } private void DisconnectIPCamera_Click(object sender, RoutedEventArgs e) { _videoViewerWpf.Stop(); _ipCamera.Disconnect(); _ipCamera.Dispose(); _connector.Disconnect(_ipCamera.VideoChannel, _provider); } 11 / 17
  • 12. Implementing the recording Extending the GUI in the xaml file Create 2 buttons that will start and stop the video capturing (after you have added the additional elements, you need to generate 2 eventhandler methods for them): <GroupBox Header="Function" Height="160" Width="542" Margin="0,360,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" > <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Button Grid.Column="0" Content="Start video capture" HorizontalAlignment="Center" VerticalAlignment="Center" Click="StartCapture_Click" Width="120" Height="50"/> <Button Grid.Column="1" Content="Stop video capture" HorizontalAlignment="Center" VerticalAlignment="Center" Click="StopCapture_Click" Width="120" Height="50"/> </Grid> </GroupBox> 12 / 17
  • 13. Implementing the recording Building the video recorder in the xaml.cs file Declare 2 new fields for video recording. These two objects are the followings: IVideoSender and MPEG4Recorder: private MPEG4Recorder _recorder; private IVideoSender _videoSender; You need to instantiate them at USB webcam connection and IP camera connection as well by inserting both of the following lines: _videoSender = _webCamera; _videoSender = _ipCamera.VideoChannel; Into the USB camera connection section: Into the IP camera connection section: 13 / 17
  • 14. Implementing the recording Building the video recorder in the xaml.cs file To differentiate the captured streams, their file name will contain the current date and time. The destination folder will be the place of the program. Instantiate the recorder and subscribe the eventhandler method for the MultiplexFinished event. Connect the videosender and the recorder: private void StartCapture_Click(object sender, RoutedEventArgs e) { if (_videoSender == null) return; var date = DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Hour + "-" + DateTime.Now.Minute + "-" + DateTime.Now.Second; var currentpath = AppDomain.CurrentDomain.BaseDirectory + date + ".mpeg4"; _recorder = new MPEG4Recorder(currentpath); _recorder.MultiplexFinished += _recorder_MultiplexFinished; _connector.Connect(_videoSender, _recorder.VideoRecorder); } 14 / 17
  • 15. Implementing the recording Building the video recorder in the xaml.cs file Create the eventhandler method. Unsubscribe from the event and dispose the recorder. To be able to stop capturing, you need to disconnect and call the Multiplex method that creates the video. void _recorder_MultiplexFinished(object sender, Ozeki.VoIP.VoIPEventArgs<bool> e) { _recorder.MultiplexFinished -= _recorder_MultiplexFinished; _recorder.Dispose(); } private void StopCapture_Click(object sender, RoutedEventArgs e) { if (_videoSender == null) return; _connector.Disconnect(_videoSender, _recorder.VideoRecorder); _recorder.Multiplex(); } 15 / 17
  • 16. Testing the application Run the application and test the video recorder Step 1: Connect to a camera • USB webcam: Click on „Connect” • IP camera: Enter the IP address of the camera, its username and password, then click on „Connect” Step 2: Start capturing Click on „Start video capture” Step 3: Finish capturing and save the video file Click on „Stop video capture” Step 4: Watch the video file 16 / 17
  • 17. Thank you for your attention! To sum it up, the ONVIF-compliant OZEKI Camera SDK provides great background support for your IP camera, ONVIF IP camera and USB webcam developments by offering prewritten components to your development environment. Building a video recorder to capture the camera stream is easy as that. For more information please visit our website: www.camera-sdk.com or send us an e-mail to: info@camera-sdk.com ! SOURCE CODE: www.camera-sdk.com