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

By , 4 Jul 2011
 

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:

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:

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

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

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

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

About the Author

Massimo Conti
Software Developer
Italy Italy
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionModify or add blank frames?membertuliosouza4-Jun-13 10:05 
QuestionHelp me. please!membernguyễn ngọc vỹ27-May-13 18:27 
GeneralMy vote of 4membernaseer baloch1-May-13 22:13 
QuestionNice Demo, but where is the audio when preview ?memberJohnson.Gao.12623-Apr-13 1:20 
Questionviewing stream on a remote computermemberMember 95262384-Apr-13 4:42 
AnswerRe: viewing stream on a remote computermemberMember 1008220412-Jun-13 10:05 
QuestionWebcam is showing black image in previewmemberMember 79864943-Apr-13 23:51 
GeneralThanks!memberwcuthbertson8-Mar-13 9:52 
QuestionI am getting black screen while previwingmembersudheendra pavan sundar8-Mar-13 1:53 
QuestionHelpmembermakhondi4-Mar-13 6:22 
QuestionError on winfows 8memberTub-9520-Feb-13 4:10 
GeneralMy vote of 5memberWanararbrio16-Jan-13 9:33 
GeneralMy vote of 5memberXtErMiNaToR10210-Jan-13 23:56 
Questionusing Microsoft.Expression.Encoder.Utilities; not found help mememberMember 964439429-Dec-12 19:10 
QuestionDoes not detect my usb Genius Camera Eye 312memberLuis Zeta4-Dec-12 5:39 
AnswerRe: Does not detect my usb Genius Camera Eye 312memberLuis Zeta4-Dec-12 6:22 
QuestionWebcam detector in c#memberMember 798053425-Nov-12 18:28 
QuestionIP cammemberrodey vincent22-Nov-12 19:17 
QuestionAdjustement with wcfmemberAndéol_0014-Nov-12 5:45 
GeneralMy vote of 4memberMember 94807894-Oct-12 16:30 
QuestionUnable to create Devicememberharmurk20-Sep-12 3:15 
QuestionHow to implement in existing projectmemberMember 943991418-Sep-12 12:43 
QuestionLive video display in c# .netmemberMember 900485711-Jul-12 18:36 
Questionincrustationmembergefkuz23-Jun-12 2:11 
Questionurgent please i need simple security camera application in c# or vb.netmemberkaydyonis21-Jun-12 8:39 
Questionexcellent!memberShejn21-Jun-12 4:34 
QuestionLiveJob [modified]memberweed2k14-Jun-12 4:06 
Questionbroadcast in internetmemberMember 802424911-Jun-12 1:16 
QuestionVideo is delay (lagging) after 1 minute running.memberrockblin7-Jun-12 16:17 
QuestionRe: Video is delay (lagging) after 1 minute running.memberrockblin7-Jun-12 17:23 
QuestionResolutionmembermurillobraga7-Jun-12 9:50 
Questionsee streamed video in web sitememberMember 80242495-Jun-12 3:47 
QuestionGreat Projectmember665909018-May-12 10:52 
QuestionCapturing Encode result to save in DataBasememberbkrussell18-May-12 3:21 
GeneralRecord Button thrown Expectionmember665909011-May-12 3:22 
GeneralRe: Record Button thrown ExpectionmemberMassimo Conti11-May-12 9:00 
I guess you have the UAC (User Accout Control) enabled.
If you need UAC enabled, try to run the application with 'Run as Administrator'.
You could also disable it and try again, or simply change where to save the stream, so NOT in C:\, but in your Document Folder.
 
I hope this helps.
QuestionVideo Delay and LagmemberMember 80802984-May-12 14:37 
GeneralMy vote of 5memberMohammad Sepahvand30-Apr-12 1:34 
GeneralRe: My vote of 5memberMohammad Sepahvand4-May-12 22:46 
GeneralMy vote of 5memberandyclass5027-Apr-12 12:13 
QuestionExcellent! but had to change location where files saved tomemberfarvenkloodgen18-Apr-12 21:43 
BugCall to PickBestVideoFormat is wrongmemberChad Z. Hower aka Kudzu28-Mar-12 4:22 
GeneralRe: Call to PickBestVideoFormat is wrongmemberMassimo Conti28-Mar-12 11:22 
GeneralRe: Call to PickBestVideoFormat is wrongmemberChad Z. Hower aka Kudzu28-Mar-12 11:25 
GeneralRe: Call to PickBestVideoFormat is wrongmemberChad Z. Hower aka Kudzu28-Mar-12 15:50 
GeneralRe: Call to PickBestVideoFormat is wrongmemberMassimo Conti30-Mar-12 9:19 
QuestionHow to get a Handle to underlying stream of the MediaElement?memberMember 871481722-Mar-12 9:54 
Questionmy vote of 5member.NetStars21-Mar-12 1:28 
QuestionCapture image from streamingmemberEldoran4-Mar-12 6:51 
AnswerRe: Capture image from streamingmemberMassimo Conti4-Mar-12 10:54 

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

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