Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

C# tutorial on how to implement IP camera device discovery (WPF)

ip camera csharp c# webcam device discover source code how to

  • Please log in to reply
No replies to this topic

#1 CurtCopeland

CurtCopeland

    CC Lurker

  • New Member
  • Pip
  • 4 posts

Posted 19 November 2014 - 04:52 AM

In this brief tutorial I will demonstrate how to build a Visual C# WPF application that can be used to discover IP cameras on LAN.  This feature can be really useful if you would like to connect to a camera (for instance in order to view its image), but you do not know its network details. Instead of using a multi-page article, I would rather present a brief C# code snippet (that was written with the help of an IP camera library) with some explanations. Do not waste time, let’s get programming started!

 

This device discovery application enables to discover all the available IP cameras that are connected to your local network and to find out their IP address and port number. To implement the user interface you need to create a button (that is used to start discovering), a combobox (to list the available IP cameras), and some labels and textboxes (to display the IP address and port number of the preferred camera).

 

The application that is presented in this tutorial can be seen on the following figure. After clicking on the Discover button, the available devices appear in the dropdown menu. If you select a camera from the list, its IP address and port number will be displayed automatically.

 

post-55804-0-53401400-1416401259.jpg

 

 

For the implementation of the GUI above, the following code can used:

<Window x:Class="Discover_Cameras_WPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="IP Camera Device Discovery" Height="206" Width="283" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen">
    <Grid>
        <GroupBox Header="Device Discovery" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="157" Width="251">
            <Grid HorizontalAlignment="Left" Height="141" VerticalAlignment="Top" Width="231" Margin="0,0,-2,-2">
                <Grid.RowDefinitions>
                    <RowDefinition Height="1*"/>
                    <RowDefinition Height="1*"/>
                    <RowDefinition Height="1*"/>
                    <RowDefinition Height="1*"/>
                </Grid.RowDefinitions>
                <Button Content="Discover" HorizontalAlignment="Left" VerticalAlignment="Center" Width="75" Click="button_Discover_Click" Margin="0,3,0,10" />
                <ComboBox x:Name="comboBox_Devices" HorizontalAlignment="Left" Grid.Row="1" VerticalAlignment="Center" Width="120" SelectionChanged="comboBox_Devices_SelectedIndexChanged" Margin="0,2,0,11" />
                <Label Content="Address" HorizontalAlignment="Left" Grid.Row="2" VerticalAlignment="Center" Margin="0,5,0,1" Height="30" />
                <Label Content="Port" HorizontalAlignment="Left" Grid.Row="2" VerticalAlignment="Center" Margin="0,28,0,11" Height="25" Width="53" Grid.RowSpan="2" />
                <TextBox x:Name="textBox_Host" HorizontalAlignment="Right" Height="22" Grid.Row="2" TextWrapping="Wrap" VerticalAlignment="Center" Width="175" IsEnabled="False" Margin="0,8,1,6" />
                <TextBox x:Name="textBox_Port" HorizontalAlignment="Right" Height="22" Grid.Row="2" TextWrapping="Wrap" VerticalAlignment="Center" Width="175" IsEnabled="False" Margin="0,30,1,12" Grid.RowSpan="2" />
            </Grid>
        </GroupBox>
    </Grid>
</Window>

After this use the following code in the MainWindow.xaml.cs file:

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Ozeki.Media.IPCamera;
using Ozeki.Media.IPCamera.Discovery;

namespace Discover_Cameras_WPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private List<DiscoveredDeviceInfo> _deviceList;

        public MainWindow()
        {
            InitializeComponent();

            IPCameraFactory.DeviceDiscovered += IPCamera_DiscoveryCompleted;
        }

        private void button_Discover_Click(object sender, RoutedEventArgs e)
        {
            _deviceList = new List<DiscoveredDeviceInfo>();

            InvokeGuiThread(() => comboBox_Devices.Items.Clear());

            IPCameraFactory.DiscoverDevices();
        }

        private void IPCamera_DiscoveryCompleted(object sender, DiscoveryEventArgs e)
        {
            _deviceList.Add(e.Device);
            InvokeGuiThread(() => comboBox_Devices.Items.Add(e.Device.Name));
        }

        private void comboBox_Devices_SelectedIndexChanged(object sender, SelectionChangedEventArgs e)
        {
            InvokeGuiThread(() =>
            {
                var selected = comboBox_Devices.SelectedIndex;
                if (selected < 0 || selected > _deviceList.Count - 1) return;
                textBox_Host.Text = _deviceList[selected].Host;
                textBox_Port.Text = _deviceList[selected].Port.ToString();
            });
        }

        private void InvokeGuiThread(Action action)
        {
            Dispatcher.BeginInvoke(action);
        }
    }
}

Note: For this project you need to add the VoIPSDK.dll to your references, otherwise some errors can occured after running the application. The dll can be found on the official website of the mentioned IP camera library.


  • 0





Also tagged with one or more of these keywords: ip, camera, csharp, c#, webcam, device, discover, source code, how to