Click here to Skip to main content
15,885,546 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 713.7K   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

 
QuestionGrab image Pin
zevuv26-Aug-11 2:30
zevuv26-Aug-11 2:30 
AnswerRe: Grab image Pin
Massimo Conti27-Aug-11 9:44
Massimo Conti27-Aug-11 9:44 
GeneralMy vote of 5 Pin
itsho21-Jul-11 10:42
itsho21-Jul-11 10:42 
GeneralMy vote of 5 Pin
descartes12-Jul-11 11:53
descartes12-Jul-11 11:53 
QuestionCompiler VS 2008...? Pin
descartes11-Jul-11 11:37
descartes11-Jul-11 11:37 
GeneralMy vote of 5 Pin
ivix4u5-Jul-11 0:10
ivix4u5-Jul-11 0:10 
QuestionMy vote of 5 Pin
Filip D'haene30-Jun-11 5:01
Filip D'haene30-Jun-11 5:01 
QuestionChanging Video Size Pin
NYCChris21-Jun-11 7:33
NYCChris21-Jun-11 7:33 
I made a small modification to your demo. I set the Anchor property on the panelVideoPreview to Top, Left, Bottom, Right. Then I added the following code in the btnPreview_Click method. This code will set the camera to record in 640x480, and resize the preview image along with the form. (at least I assume this is what this does.)

var size = new Size(640, 480);
_deviceSource.PickBestVideoFormat(size, 1);
_deviceSource.PreviewWindow = new PreviewWindow(new HandleRef(panelVideoPreview, panelVideoPreview.Handle));
panelVideoPreview.Resize += (s, a) => _deviceSource.PreviewWindow.SetSize(panelVideoPreview.Size);


Thank you for all the effort you put into your article! It really saved me quite a bit of work!
AnswerRe: Changing Video Size Pin
Massimo Conti21-Jun-11 10:04
Massimo Conti21-Jun-11 10:04 
Generalgetting error upon selecting "Start Recording" Pin
acheoacheo14-Jun-11 13:58
acheoacheo14-Jun-11 13:58 
GeneralRe: getting error upon selecting "Start Recording" Pin
Massimo Conti15-Jun-11 8:56
Massimo Conti15-Jun-11 8:56 
GeneralI can't wait to use this! :) Pin
Anthony Daly12-Jun-11 12:23
Anthony Daly12-Jun-11 12:23 
Generalgreat article [modified] Pin
twinscythe123327-Jun-11 22:27
twinscythe123327-Jun-11 22:27 
GeneralRe: great article Pin
Massimo Conti8-Jun-11 19:27
Massimo Conti8-Jun-11 19:27 
GeneralRe: great article Pin
twinscythe123328-Jun-11 20:03
twinscythe123328-Jun-11 20:03 
GeneralRe: great article Pin
Massimo Conti12-Jun-11 10:33
Massimo Conti12-Jun-11 10:33 
GeneralRe: great article Pin
twinscythe1233212-Jun-11 20:21
twinscythe1233212-Jun-11 20:21 
GeneralMy vote of 2 Pin
Shahab Addin4-Jun-11 4:52
Shahab Addin4-Jun-11 4:52 
GeneralRe: My vote of 2 Pin
Massimo Conti5-Jun-11 11:51
Massimo Conti5-Jun-11 11:51 
GeneralRe: My vote of 2 Pin
Shahab Addin5-Jun-11 22:25
Shahab Addin5-Jun-11 22:25 
GeneralRe: My vote of 2 Pin
Massimo Conti12-Jun-11 10:48
Massimo Conti12-Jun-11 10:48 
GeneralRe: My vote of 2 [modified] Pin
Massimo Conti5-Jun-11 12:04
Massimo Conti5-Jun-11 12:04 
GeneralRe: My vote of 2 Pin
Shahab Addin5-Jun-11 22:11
Shahab Addin5-Jun-11 22:11 
GeneralRe: My vote of 2 Pin
Massimo Conti6-Jun-11 11:03
Massimo Conti6-Jun-11 11:03 
NewsAnother solution Pin
Super Lloyd1-Jun-11 18:37
Super Lloyd1-Jun-11 18:37 

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.