How to connect to an ONVIF camera and display the image in C#
In the following article you will find detailed information on how to connect to an IP camera device with your Windows Forms/WPF Application written in C#. To implement this example, you need to have OZEKI Camera SDK installed, and a reference to OzekiSDK.dll should be added to your Visual Studio project.
How to connect to an IP camera device using C#?
Windows Form | WPF |
Windows forms version
In order to connect to your camera device with the OZEKI Camera SDK you need to use the classes of the SDK whose objects have been declared at the beginning of the Form1 class. After this you should initialize these objects in the constructor of the form( it is possible within the declaration statements as well). The final steps are to call the method named SetVideoViewer() and to add your VideoViewerFM object to the control collection of the panel that you can see on the GUI. Important: you should study this article in order to find out how to setup your Windows Forms Application correctly.
Getting started
To get started it is recomended to Download and Install the latest version of Ozeki Camera SDK. After installation you can find the example code discussed in this page with full source code in the following location on your harddisk:
Download Ozeki Camera SDK: | https://camera-sdk.com/p_6513-download-onvif-ozeki-camera-sdk-for-c-sharp.html |
Windows forms version: | C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Other\Camera_Viewer_Connect_ONVIF_WF\Camera_Viewer_Connect_ONVIF_WF.sln |
To compile this example you will need Microsoft Visual Studio installed on your computer.
The following Method calls which can react to the "Connect button pushed" event:
_camera=new IPCamera("192.168.112.109:8080","user","qwe123"): this method initializes the camera device that has been declared as a private member of the class. The used arguments are the following: first, we need the IP address of the device, then the necessary username and password.
_connector.Connect(_camera.VideoChannel, _imageProvider): with the help of this method the connection between the image that we recive from our IP camera and the image provider object which is used for displaying the image of the camera on the Graphical User Interface is established.
_camera.Start(): you should use this method in order to receive image from the camera.
_videoViewerWF1.Start(): with the help of this viewer the image on the GUI will be displayed.
IP camera connection example in C#
Form1.cs:
using System; using System.Windows.Forms; using Ozeki.Media; using Ozeki.Camera; namespace VideoCameraViewer03 { public partial class Form1 : Form { private IIPCamera _camera; private DrawingImageProvider _imageProvider = new DrawingImageProvider(); private MediaConnector _connector = new MediaConnector(); private VideoViewerWF _videoViewerWF1; public Form1() { InitializeComponent(); // Create video viewer UI control _videoViewerWF1 = new VideoViewerWF(); _videoViewerWF1.Name = "videoViewerWF1"; _videoViewerWF1.Size = panel1.Size; panel1.Controls.Add(_videoViewerWF1); // Bind the camera image to the UI control _videoViewerWF1.SetImageProvider(_imageProvider); } // Connect camera video channel to image provider and start private void connectBtn_Click(object sender, EventArgs e) { _camera = IPCameraFactory.GetCamera("rtsp://192.168.113.150:554/ufirststream", "root", "pass"); _connector.Connect(_camera.VideoChannel, _imageProvider); _camera.Start(); _videoViewerWF1.Start(); } } }
Code 1 - IP camera connection example in C#
Please note that none of the cancel and disconnect methods are included in the example because of the demonstrating intent and briefness of the article.
GUI
After the successful implementation of the functions and the GUI elements, the application will work properly. By pressing the connect button the image of the IP camera will be loaded in.
Below you can find the code that belongs to the interface of the previously presented application. With the help of this section your Windows Forms Application will be able to work properly.
Form1.Designer.cs
namespace VideoCameraViewer03 { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.connectBtn = new System.Windows.Forms.Button(); this.panel1 = new System.Windows.Forms.Panel(); this.SuspendLayout(); // // connectBtn // this.connectBtn.Location = new System.Drawing.Point(10, 230); this.connectBtn.Name = "connectBtn"; this.connectBtn.Size = new System.Drawing.Size(75, 23); this.connectBtn.TabIndex = 2; this.connectBtn.Text = "Connect"; this.connectBtn.UseVisualStyleBackColor = true; this.connectBtn.Click += new System.EventHandler(this.connectBtn_Click); // // panel1 // this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.panel1.Location = new System.Drawing.Point(10, 10); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(250, 210); this.panel1.TabIndex = 3; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(274, 264); this.Controls.Add(this.panel1); this.Controls.Add(this.connectBtn); this.Name = "Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "IP Camera\'s Live Image"; this.ResumeLayout(false); } #endregion private System.Windows.Forms.Button connectBtn; private System.Windows.Forms.Panel panel1; } }
Code 2 - GUI example in C#
WPF version
In order to connect to your camera device with the OZEKI Camera SDK you need to use the classes of the SDK whose objects have been declared at the beginning of the MainWindow class. After this you should initialize these objects in the constructor of the form( it is possible within the declaration statements as well). Important: you should study this article in order to find out how to setup your WPF Application correctly.
Getting started
To get started it is recomended to Download and Install the latest version of Ozeki Camera SDK. After installation you can find the example code discussed in this page with full source code in the following location on your harddisk:
Download Ozeki Camera SDK: | https://camera-sdk.com/p_6513-download-onvif-ozeki-camera-sdk-for-c-sharp.html |
WPF version: | C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Other\Camera_Viewer_Connect_ONVIF_WPF\Camera_Viewer_Connect_ONVIF_WPF.sln |
To compile this example you will need Microsoft Visual Studio installed on your computer.
The following Method calls which can react to the "Connect button pushed" event:
_camera=new IPCamera("192.168.112.109:8080","user","qwe123"): this method initializes the camera device that has been declared as a private member of the class. The used arguments are the following: first, we need the IP address of the device, then the necessary username and password.
_connector.Connect(_camera.VideoChannel, _drawingImageProvider): with the help of this method the connection between the image that we recive from our IP camera and the image provider object which is used for displaying the image of the camera on the Graphical User Interface is established.
_camera.Start(): you should use this method in order to receive image from the camera.
videoViewer.Start(): with the help of this viewer the image on the GUI will be displayed.
IP camera connection example in C#
MainWindow.xaml.cs
using System.Windows; using Ozeki.Media; using Ozeki.Camera; namespace VideoCameraViewer03Wpf { public partial class MainWindow : Window { private IIPCamera _camera; private DrawingImageProvider _drawingImageProvider; private MediaConnector _connector; public MainWindow() { InitializeComponent(); _drawingImageProvider = new DrawingImageProvider(); _connector = new MediaConnector(); videoViewer.SetImageProvider(_drawingImageProvider); } private void Connect_Click(object sender, RoutedEventArgs e) { _camera = new IPCamera("192.168.112.109:8080", "user", "qwe123"); _connector.Connect(_camera.VideoChannel, _drawingImageProvider); _camera.Start(); videoViewer.Start(); } } }
Code 1 - IP camera connection example in C#
Please note that none of the cancel and disconnect methods are included in the example because of the demonstrating intent and briefness of the article.
GUI
After the successful implementation of the functions and the GUI elements, the application will work properly. By pressing the connect button the image of the IP camera will be loaded in.
Below you can find the code that belongs to the interface of the previously presented application. With the help of this section your WPF Application will be able to work properly.
MainWindow.xaml
<Window x:Class="VideoCameraViewer03Wpf.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:controls="clr-namespace:Ozeki.Media;assembly=OzekiSDK" Title="IP camera's live image" Height="305" Width="336" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen"> <Grid> <GroupBox Header="Live camera" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="226" Width="308"> <Grid HorizontalAlignment="Left" Height="204" VerticalAlignment="Top" Width="296"> <controls:VideoViewerWPF Name="videoViewer" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Black"/> </Grid> </GroupBox> <Button Content="Connect" HorizontalAlignment="Left" Margin="10,241,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.107,-0.364" Click="Connect_Click"/> </Grid> </Window>
DISCLAIMER: Please note that the following features will only work if your IP camera supports the given function. You should check the user manual of your IP camera to make sure it supports the feature that you wish to implement in C#.
Related Pages
FAQ
Below you can find the answers for the most frequently asked questions related to this topic:
-
How can I get the URL of the camera?
You can get the URL from the producer of the camera. (In the 10th tutorial you can find information on how to create an own IP camera discoverer program.)
-
I have not managed to build the solution. How to solve it?
- Please set the Target framework property of the project to .NET 4.0.
- You should add the System.Drawing.dll and the OzekiSDK.dll to the references of the solution.
- Please import the missing classes.
-
What's the difference among USB camera, RTSP camera and ONVIF camera?
In the case of USB camera we do not need camera url and some functions(like moving, zooming, etc.) are not available. In the case of RTSP camera and ONVIF camera there are more available functions. The difference between the RTSP camera and the ONVIF camera is while the RTSP camera can have only one stream at a time, the ONVIF camera can have multiple.
More information
- How to connect to an USB camera and display the picture in C#
- How to connect to an RTSP camera and display the picture in C#
- How to connect to an ONVIF camera and display the image in C#
- C# Play audio from RTSP/ONVIF camera
- How to send audio to a camera in C#
- Query the audio video frame rate resolution codec
- How to resize the picture of a broadcasted camera for displaying in C#
- How to mirror the camera picture vertically and horizontally in C#
- Setup camera image Brightness Saturation Contrast values in C#
- How to control the frame rate in C#
- How to setup the white balance in C#
- How to use backlight compensation in C#
- Change the video resolution
- Concat multiple video source in C#