Click here to Skip to main content
15,881,424 members
Articles / Programming Languages / C#

How to use a web cam in C# with .NET Framework 4.0 and Microsoft Expression Encoder 4

Rate me:
Please Sign up or sign in to vote.
4.86/5 (77 votes)
4 Jul 2011CPOL1 min read 712.3K   39.5K   190   157
How to use a webcam in C#.

Screenshot_small.jpg

Introduction

If you are interested in using your webcam from C# in an easy way, this little article is for you. In order to achieve our goal, we need Microsoft .NET 4.0 and Microsoft Expression Encoder 4. You can get the latter for free, using the 'Microsoft Web Platform Installer', that can be downloaded from here: http://www.microsoft.com/web/downloads/platform.aspx.

Web_Platform_Installer_small.jpg

After that, we need to create a Windows Forms application and add the following references to the project:

Assembly Microsoft.Expression.Encoder
C:\Program Files\Microsoft Expression\Encoder 4\SDK\Microsoft.Expression.Encoder.dll

Assembly Microsoft.Expression.Encoder.Utilities
C:\Program Files\Microsoft Expression\Encoder 4\SDK\Microsoft.Expression.Encoder.Utilities.dll

Assembly Microsoft.Expression.Encoder.Types
C:\Program Files\Microsoft Expression\Encoder 4\SDK\Microsoft.Expression.Encoder.Types.dll

Using the Code

Here is the code to enumerate the video and audio devices:

C#
foreach (EncoderDevice edv in EncoderDevices.FindDevices(EncoderDeviceType.Video))
{
     lstVideoDevices.Items.Add(edv.Name);
}
foreach (EncoderDevice eda in EncoderDevices.FindDevices(EncoderDeviceType.Audio))
{
     lstAudioDevices.Items.Add(eda.Name);
}

Here is the code to preview the video and the audio:

C#
// Starts new job for preview window
_job = new LiveJob();

// Create a new device source. We use the first audio and video devices on the system
_deviceSource = _job.AddDeviceSource(video, audio);

// Sets preview window to winform panel hosted by xaml window
_deviceSource.PreviewWindow = new PreviewWindow(new HandleRef(panel1, panel1.Handle));

// Make this source the active one
_job.ActivateSource(_deviceSource);

Here is the code to record the video and audio to a .wmv file:

C#
// Sets up publishing format for file archival type
FileArchivePublishFormat fileOut = new FileArchivePublishFormat();

// Sets file path and name
fileOut.OutputFileName = String.Format("C:\\WebCam{0:yyyyMMdd_hhmmss}.wmv", DateTime.Now);

// Adds the format to the job. You can add additional formats
// as well such as Publishing streams or broadcasting from a port
_job.PublishFormats.Add(fileOut);

// Start encoding
_job.StartEncoding();

Streaming the webcam over the network

Here is the code to stream the video (and audio) of your webcam over the network:

C#
// Sets up publishing format for file archival type
_job = new LiveJob();

_deviceSource = _job.AddDeviceSource(video, audio);
_job.ActivateSource(_deviceSource);         

// Finds and applys a smooth streaming preset        
_job.ApplyPreset(LivePresets.VC1256kDSL16x9);

// Creates the publishing format for the job
PullBroadcastPublishFormat format = new PullBroadcastPublishFormat();
format.BroadcastPort = 8080;
format.MaximumNumberOfConnections = 2;

// Adds the publishing format to the job
_job.PublishFormats.Add(format);

// Starts encoding
_job.StartEncoding();

To view the broadcast, you can create a WPF application and use the MediaElement. It is just one line of code! Here is the whole code of the WPF application:

XML
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Show Broadcast" Height="350" Width="525">
    <Grid>
        <MediaElement Name="VideoControl" Source="http://localhost:8080" />
    </Grid>
</Window>

Conclusion

A piece of cake, isn't it? This is the new frontier of how to manage video and audio from C#. In the early days of C#, using a webcam was not so easy. I hope this will help those who want to play a bit with a webcam. Thanks for reading my first article :-)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 4 Pin
naseer baloch1-May-13 22:13
naseer baloch1-May-13 22:13 
QuestionNice Demo, but where is the audio when preview ? Pin
Johnson.Gao.12623-Apr-13 1:20
Johnson.Gao.12623-Apr-13 1:20 
Questionviewing stream on a remote computer Pin
Member 95262384-Apr-13 4:42
Member 95262384-Apr-13 4:42 
AnswerRe: viewing stream on a remote computer Pin
Member 1008220412-Jun-13 10:05
Member 1008220412-Jun-13 10:05 
GeneralRe: viewing stream on a remote computer Pin
Member 1107374812-Jan-17 7:51
Member 1107374812-Jan-17 7:51 
QuestionWebcam is showing black image in preview Pin
Member 79864943-Apr-13 23:51
Member 79864943-Apr-13 23:51 
AnswerRe: Webcam is showing black image in preview Pin
Member 202563013-Jun-14 21:15
Member 202563013-Jun-14 21:15 
GeneralThanks! Pin
wcuthbertson8-Mar-13 9:52
wcuthbertson8-Mar-13 9:52 
Nice demo! I have been searching for some example C# code for several days to help me manage my webcam for a work project. I was amazed at how few good examples were to be had, as I require both Video AND Audio capture. Most examples just do video or require 3rd party software or were actually wrappers from C++ (which I know, but not as well as C#). Your example doesn't do exactly what I need, but at least you got me started and hopefully I can find the remaining answers in the MS Expression Encoder docs.
My only suggestion would be that you post a link to those docs on this page, but I'm guessing I can find them without much trouble. Thanks again!
QuestionI am getting black screen while previwing Pin
sudheendra pavan sundar8-Mar-13 1:53
sudheendra pavan sundar8-Mar-13 1:53 
QuestionHelp Pin
makhondi4-Mar-13 6:22
makhondi4-Mar-13 6:22 
QuestionError on winfows 8 Pin
Tub-9520-Feb-13 4:10
Tub-9520-Feb-13 4:10 
AnswerRe: Error on winfows 8 Pin
Member 44347192-Apr-14 8:40
Member 44347192-Apr-14 8:40 
GeneralMy vote of 5 Pin
Wanararbrio16-Jan-13 9:33
Wanararbrio16-Jan-13 9:33 
GeneralMy vote of 5 Pin
XtErMiNaToR10210-Jan-13 23:56
XtErMiNaToR10210-Jan-13 23:56 
Questionusing Microsoft.Expression.Encoder.Utilities; not found help me Pin
Sudhir Kumar Solka29-Dec-12 19:10
Sudhir Kumar Solka29-Dec-12 19:10 
QuestionDoes not detect my usb Genius Camera Eye 312 Pin
Luis Zeta4-Dec-12 5:39
Luis Zeta4-Dec-12 5:39 
AnswerRe: Does not detect my usb Genius Camera Eye 312 Pin
Luis Zeta4-Dec-12 6:22
Luis Zeta4-Dec-12 6:22 
QuestionWebcam detector in c# Pin
Member 798053425-Nov-12 18:28
Member 798053425-Nov-12 18:28 
QuestionIP cam Pin
rodey vincent22-Nov-12 19:17
rodey vincent22-Nov-12 19:17 
QuestionAdjustement with wcf Pin
Andéol_0014-Nov-12 5:45
Andéol_0014-Nov-12 5:45 
GeneralMy vote of 4 Pin
Member 94807894-Oct-12 16:30
Member 94807894-Oct-12 16:30 
QuestionUnable to create Device Pin
harmurk20-Sep-12 3:15
harmurk20-Sep-12 3:15 
QuestionHow to implement in existing project Pin
Member 943991418-Sep-12 12:43
Member 943991418-Sep-12 12:43 
QuestionLive video display in c# .net Pin
Member 900485711-Jul-12 18:36
Member 900485711-Jul-12 18:36 
Questionincrustation Pin
gefkuz23-Jun-12 2:11
gefkuz23-Jun-12 2:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.