Click here to Skip to main content
16,012,110 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 749.1K   39.6K   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

 
GeneralRe: Another solution Pin
Massimo Conti1-Jun-11 21:16
Massimo Conti1-Jun-11 21:16 
GeneralRe: Another solution Pin
Super Lloyd1-Jun-11 21:28
Super Lloyd1-Jun-11 21:28 
GeneralRe: Another solution Pin
Massimo Conti1-Jun-11 22:17
Massimo Conti1-Jun-11 22:17 
GeneralRe: Another solution [modified] Pin
Super Lloyd1-Jun-11 22:24
Super Lloyd1-Jun-11 22:24 
GeneralRe: Another solution Pin
Super Lloyd1-Jun-11 22:26
Super Lloyd1-Jun-11 22:26 
GeneralRe: Another solution Pin
Massimo Conti2-Jun-11 2:56
Massimo Conti2-Jun-11 2:56 
GeneralRe: Another solution Pin
Super Lloyd2-Jun-11 4:00
Super Lloyd2-Jun-11 4:00 
GeneralRe: Another solution Pin
Massimo Conti2-Jun-11 5:03
Massimo Conti2-Jun-11 5:03 
As I say I really like AForge.NET and also its deployment.
My solution is for desktop PC, not embedded PC, and it is not an issue the deployment on this platform.
It seems to me that your difficulty for embedded PC is the connection speed and the possibility to deploy.
This does not have nothing to do with the deployment itself to the embedded device.
I didn't try and i'm not interested, but i think that could be tecnically possible to redist only the runtime of EE4 with the xcopy.
I repeat the deployment is out of the scope of this article. Thanks for your comments.
I don't see why you told me "I am entitled to my opinion". Of course you are and I agree with you.
GeneralRe: Another solution Pin
Super Lloyd1-Jun-11 21:37
Super Lloyd1-Jun-11 21:37 
Generalgets my vote Pin
Sacha Barber31-May-11 1:17
Sacha Barber31-May-11 1:17 
GeneralRe: gets my vote [modified] Pin
Massimo Conti31-May-11 1:57
Massimo Conti31-May-11 1:57 
GeneralRe: gets my vote Pin
Sacha Barber31-May-11 3:24
Sacha Barber31-May-11 3:24 
GeneralRe: gets my vote Pin
Super Lloyd1-Jun-11 18:41
Super Lloyd1-Jun-11 18:41 
GeneralRe: gets my vote Pin
Massimo Conti1-Jun-11 21:23
Massimo Conti1-Jun-11 21:23 
GeneralRe: gets my vote Pin
Super Lloyd1-Jun-11 21:28
Super Lloyd1-Jun-11 21:28 
Generalvs . WIA vs. TWAIN Pin
Yvan Rodrigues30-May-11 14:37
professionalYvan Rodrigues30-May-11 14:37 
GeneralRe: vs . WIA vs. TWAIN Pin
Massimo Conti30-May-11 19:32
Massimo Conti30-May-11 19:32 
GeneralMy vote of 4 Pin
pne@hetnet.nl30-May-11 0:39
pne@hetnet.nl30-May-11 0:39 
GeneralRe: My vote of 4 Pin
Massimo Conti30-May-11 10:15
Massimo Conti30-May-11 10:15 
GeneralRe: My vote of 4 Pin
CSharpByDesign3-Jun-11 9:40
CSharpByDesign3-Jun-11 9:40 
GeneralRe: My vote of 4 Pin
Massimo Conti12-Jun-11 11:14
Massimo Conti12-Jun-11 11:14 

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.