Click here to Skip to main content
14,173,165 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


189 bookmarked
Posted 27 May 2011
Licenced CPOL

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

, 4 Jul 2011
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 :-)


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

You may also be interested in...


Comments and Discussions

QuestionBug in the demo Pin
Member 1361394030-Aug-18 23:28
memberMember 1361394030-Aug-18 23:28 
QuestionWPF demo is empty, is a joke? Pin
Edison J Romo R21-Jun-18 10:24
memberEdison J Romo R21-Jun-18 10:24 
QuestionAudio does not work Pin
Member 135588078-Dec-17 0:13
memberMember 135588078-Dec-17 0:13 
Questiongetting error "The type or namespace name 'Expression' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)" Pin
Member 106417797-Nov-16 19:17
memberMember 106417797-Nov-16 19:17 
QuestionError on EncoderDevices.FindDevices(EncoderDeviceType.Video) Pin
Manuel Eduardo Canepa Cihuelo24-Sep-16 7:57
professionalManuel Eduardo Canepa Cihuelo24-Sep-16 7:57 
QuestionError when live camera Pin
Member 1019731424-Aug-16 22:56
memberMember 1019731424-Aug-16 22:56 
Questionpreview window always got still image or black image Pin
arfken030917-May-16 20:46
memberarfken030917-May-16 20:46 
QuestionStrange behaviour Pin
Zmuro16-Oct-15 2:38
memberZmuro16-Oct-15 2:38 
QuestionHow can I access the video from a web page? Pin
Member 113118281-Mar-15 12:01
memberMember 113118281-Mar-15 12:01 
QuestionSystem.Deployment.Application.InvalidDeploymentException Pin
Jirka Šáda2-Feb-15 5:18
memberJirka Šáda2-Feb-15 5:18 
QuestionScreen Capture Pin
fazilamehtab13-Oct-14 22:24
memberfazilamehtab13-Oct-14 22:24 
AnswerRe: Screen Capture Pin
Mehdi Rostami22-Oct-14 8:30
memberMehdi Rostami22-Oct-14 8:30 
QuestionPlease Help me Pin
AlbertusChW9-Sep-14 6:22
memberAlbertusChW9-Sep-14 6:22 
QuestionHey there. Pin
Member 104321188-Jun-14 4:21
memberMember 104321188-Jun-14 4:21 
Questionstart stop recording button only makes one file which is latest recorded Pin
Member 801762717-Apr-14 20:55
memberMember 801762717-Apr-14 20:55 
AnswerRe: start stop recording button only makes one file which is latest recorded Pin
Member 111690743-Nov-14 22:03
memberMember 111690743-Nov-14 22:03 
Questionstart encoding Pin
badi2386-Apr-14 21:57
memberbadi2386-Apr-14 21:57 
AnswerRe: start encoding Pin
HugoAmi23-Sep-14 16:02
memberHugoAmi23-Sep-14 16:02 
Generalwebcam capture Pin
rakdosss2-Apr-14 5:52
memberrakdosss2-Apr-14 5:52 
QuestionStrong Delay Pin
erebos00717-Feb-14 12:42
membererebos00717-Feb-14 12:42 
AnswerRe: Strong Delay Pin
selfwalker23-Jun-14 23:33
memberselfwalker23-Jun-14 23:33 
GeneralRe: Strong Delay Pin
Amir No-Family14-Jun-17 3:41
memberAmir No-Family14-Jun-17 3:41 
QuestionCould not load file or assembly 'Microsoft.Expression.Encoder, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. An attempt was made to load a program with an incorrect format. Pin
Ian Vic Damacio8-Feb-14 22:11
memberIan Vic Damacio8-Feb-14 22:11 
SuggestionRe: Could not load file or assembly 'Microsoft.Expression.Encoder, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. An attempt was made to load a program with an incorrect format. Pin
Diego G. Fritz31-Jul-14 7:41
professionalDiego G. Fritz31-Jul-14 7:41 
QuestionAccess is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) Pin
Ian Vic Damacio24-Jan-14 22:00
memberIan Vic Damacio24-Jan-14 22:00 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190524.3 | Last Updated 4 Jul 2011
Article Copyright 2011 by Massimo Conti
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid