Click here to Skip to main content
Licence CPOL
First Posted 27 May 2011
Views 44,302
Downloads 5,158
Bookmarked 93 times

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

By | 4 Jul 2011 | Article
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:

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

Member



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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionGreat Project Pinmember665909010:52 18 May '12  
QuestionCapturing Encode result to save in DataBase Pinmemberbkrussell3:21 18 May '12  
GeneralRecord Button thrown Expection Pinmember66590903:22 11 May '12  
GeneralRe: Record Button thrown Expection PinmemberMassimo Conti9:00 11 May '12  
QuestionVideo Delay and Lag PinmemberMember 808029814:37 4 May '12  
GeneralMy vote of 5 PinmemberMohammad Sepahvand1:34 30 Apr '12  
GeneralRe: My vote of 5 PinmemberMohammad Sepahvand22:46 4 May '12  
GeneralMy vote of 5 Pinmemberandyclass5012:13 27 Apr '12  
QuestionExcellent! but had to change location where files saved to Pinmemberfarvenkloodgen21:43 18 Apr '12  
BugCall to PickBestVideoFormat is wrong PinmemberChad Z. Hower aka Kudzu4:22 28 Mar '12  
GeneralRe: Call to PickBestVideoFormat is wrong PinmemberMassimo Conti11:22 28 Mar '12  
GeneralRe: Call to PickBestVideoFormat is wrong PinmemberChad Z. Hower aka Kudzu11:25 28 Mar '12  
GeneralRe: Call to PickBestVideoFormat is wrong PinmemberChad Z. Hower aka Kudzu15:50 28 Mar '12  
GeneralRe: Call to PickBestVideoFormat is wrong PinmemberMassimo Conti9:19 30 Mar '12  
QuestionHow to get a Handle to underlying stream of the MediaElement? PinmemberMember 87148179:54 22 Mar '12  
Questionmy vote of 5 Pinmember.NetStars1:28 21 Mar '12  
QuestionCapture image from streaming PinmemberEldoran6:51 4 Mar '12  
AnswerRe: Capture image from streaming PinmemberMassimo Conti10:54 4 Mar '12  
GeneralRe: Capture image from streaming PinmemberIvo Štimac3:54 16 Mar '12  
GeneralRe: Capture image from streaming PinmemberChad Z. Hower aka Kudzu3:17 28 Mar '12  
GeneralRe: Capture image from streaming PinmemberMassimo Conti10:36 28 Mar '12  
GeneralRe: Capture image from streaming PinmemberChad Z. Hower aka Kudzu11:02 28 Mar '12  
GeneralMy vote of 5 PinmemberEldoran1:30 1 Mar '12  
GeneralMy vote of 5 PinmemberAbinash Bishoyi22:35 28 Feb '12  
GeneralMy vote of 5 PinmemberEldoran9:42 27 Feb '12  

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.

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