Click here to Skip to main content
15,881,852 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.4K   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

 
GeneralRe: Call to PickBestVideoFormat is wrong Pin
Massimo Conti30-Mar-12 9:19
Massimo Conti30-Mar-12 9:19 
QuestionHow to get a Handle to underlying stream of the MediaElement? Pin
Member 871481722-Mar-12 9:54
Member 871481722-Mar-12 9:54 
Questionmy vote of 5 Pin
.NetStars21-Mar-12 1:28
.NetStars21-Mar-12 1:28 
QuestionCapture image from streaming Pin
Eldoran4-Mar-12 6:51
Eldoran4-Mar-12 6:51 
AnswerRe: Capture image from streaming Pin
Massimo Conti4-Mar-12 10:54
Massimo Conti4-Mar-12 10:54 
GeneralRe: Capture image from streaming Pin
Ivo Štimac16-Mar-12 3:54
Ivo Štimac16-Mar-12 3:54 
GeneralRe: Capture image from streaming Pin
Chad Z. Hower aka Kudzu28-Mar-12 3:17
Chad Z. Hower aka Kudzu28-Mar-12 3:17 
GeneralRe: Capture image from streaming Pin
Massimo Conti28-Mar-12 10:36
Massimo Conti28-Mar-12 10:36 
Hello Chad Z. Hower aka Kudzu,

Yes that code does the grab image.
The problem is that if there is a windows over the panel (for example the notepad), you get it and not the picture that is below. May be the same problem could occur if the application is minimized and you want to grab the picture.
I hopw now you got the issue.

Regards,

Massimo.
GeneralRe: Capture image from streaming Pin
Chad Z. Hower aka Kudzu28-Mar-12 11:02
Chad Z. Hower aka Kudzu28-Mar-12 11:02 
GeneralMy vote of 5 Pin
Eldoran1-Mar-12 1:30
Eldoran1-Mar-12 1:30 
GeneralMy vote of 5 Pin
Abinash Bishoyi28-Feb-12 22:35
Abinash Bishoyi28-Feb-12 22:35 
GeneralMy vote of 5 Pin
Eldoran27-Feb-12 9:42
Eldoran27-Feb-12 9:42 
QuestionStrange problem Pin
cxycxy16-Jan-12 18:26
cxycxy16-Jan-12 18:26 
GeneralMy vote of 5 Pin
DaveX8614-Jan-12 3:16
DaveX8614-Jan-12 3:16 
GeneralMy vote of 5 Pin
wsc091812-Jan-12 3:06
wsc091812-Jan-12 3:06 
QuestionHow can I mirror a video stream using EE SDK? Pin
A_CAN8-Jan-12 22:29
A_CAN8-Jan-12 22:29 
GeneralMy vote of 5 Pin
lakehousetech8-Jan-12 8:51
lakehousetech8-Jan-12 8:51 
GeneralWorks Great.. Excellent Piece of Code! Thank you! Pin
jontiefer12-Dec-11 18:48
jontiefer12-Dec-11 18:48 
Question[My vote of 2] Crashes with my Logitech HD Webcam C310 Pin
jontiefer12-Dec-11 6:44
jontiefer12-Dec-11 6:44 
AnswerRe: [My vote of 2] Crashes with my Logitech HD Webcam C310 Pin
Massimo Conti12-Dec-11 10:49
Massimo Conti12-Dec-11 10:49 
QuestionChange .wmv to .avi Pin
Rdechaux12-Nov-11 3:09
Rdechaux12-Nov-11 3:09 
QuestionSo How to recieve that stream not only in localhost ? like 192.168.X.X ? :/ Pin
warefcool2-Nov-11 14:38
warefcool2-Nov-11 14:38 
Questioncould you please convert this wonderful code in VB.NET Pin
swaby23-Oct-11 16:05
swaby23-Oct-11 16:05 
AnswerRe: could you please convert this wonderful code in VB.NET Pin
Massimo Conti25-Oct-11 9:39
Massimo Conti25-Oct-11 9:39 
GeneralRe: could you please convert this wonderful code in VB.NET Pin
swaby25-Oct-11 12:02
swaby25-Oct-11 12:02 

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.