Click here to Skip to main content
Click here to Skip to main content

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

, 4 Jul 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
How to use a webcam in C#.



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:


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:

foreach (EncoderDevice edv in EncoderDevices.FindDevices(EncoderDeviceType.Video))
foreach (EncoderDevice eda in EncoderDevices.FindDevices(EncoderDeviceType.Audio))

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

// 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

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

// 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

// Start encoding

Streaming the webcam over the network

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

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

_deviceSource = _job.AddDeviceSource(video, audio);

// Finds and applys a smooth streaming preset        

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

// Adds the publishing format to the job

// Starts encoding

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:

<Window x:Class="WpfApplication1.MainWindow"
        Title="Show Broadcast" Height="350" Width="525">
        <MediaElement Name="VideoControl" Source="http://localhost:8080" />


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 Smile | :)


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


About the Author

Massimo Conti
Software Developer
Italy Italy
No Biography provided

Comments and Discussions

QuestionLive video display in c# .net PinmemberMember 900485711-Jul-12 19:36 
Questionincrustation Pinmembergefkuz23-Jun-12 3:11 
Questionurgent please i need simple security camera application in c# or Pinmemberkaydyonis21-Jun-12 9:39 
Questionexcellent! PinmemberShejn21-Jun-12 5:34 
QuestionLiveJob [modified] Pinmemberweed2k14-Jun-12 5:06 
Questionbroadcast in internet PinmemberMember 802424911-Jun-12 2:16 
QuestionVideo is delay (lagging) after 1 minute running. Pinmemberrockblin7-Jun-12 17:17 
QuestionRe: Video is delay (lagging) after 1 minute running. Pinmemberrockblin7-Jun-12 18:23 
One more thing, when I compile successful and I run it ok on my laptop (my webcam is actived). But I copy an exe file to another laptop, it doesn't work ? Do I need to install Microsoft Expression Encoder 4 into that laptop? Is it necessary?
QuestionResolution Pinmembermurillobraga7-Jun-12 10:50 
Questionsee streamed video in web site PinmemberMember 80242495-Jun-12 4:47 
QuestionGreat Project Pinmember665909018-May-12 11:52 
QuestionCapturing Encode result to save in DataBase Pinmemberbkrussell18-May-12 4:21 
GeneralRecord Button thrown Expection Pinmember665909011-May-12 4:22 
GeneralRe: Record Button thrown Expection PinmemberMassimo Conti11-May-12 10:00 
QuestionVideo Delay and Lag PinmemberMember 80802984-May-12 15:37 
GeneralMy vote of 5 PinmemberMohammad Sepahvand30-Apr-12 2:34 
GeneralRe: My vote of 5 PinmemberMohammad Sepahvand4-May-12 23:46 
GeneralMy vote of 5 Pinmemberandyclass5027-Apr-12 13:13 
QuestionExcellent! but had to change location where files saved to Pinmemberfarvenkloodgen18-Apr-12 22:43 
BugCall to PickBestVideoFormat is wrong PinmemberChad Z. Hower aka Kudzu28-Mar-12 5:22 
GeneralRe: Call to PickBestVideoFormat is wrong PinmemberMassimo Conti28-Mar-12 12:22 
GeneralRe: Call to PickBestVideoFormat is wrong PinmemberChad Z. Hower aka Kudzu28-Mar-12 12:25 
GeneralRe: Call to PickBestVideoFormat is wrong PinmemberChad Z. Hower aka Kudzu28-Mar-12 16:50 
GeneralRe: Call to PickBestVideoFormat is wrong PinmemberMassimo Conti30-Mar-12 10:19 
QuestionHow to get a Handle to underlying stream of the MediaElement? PinmemberMember 871481722-Mar-12 10:54 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150414.1 | Last Updated 4 Jul 2011
Article Copyright 2011 by Massimo Conti
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid