Click here to Skip to main content
14,176,870 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

556K views
35.9K downloads
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#.

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)

Share

About the Author

Massimo Conti
Software Developer
Italy Italy
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
AnswerRe: [My vote of 2] Crashes with my Logitech HD Webcam C310 Pin
Massimo Conti12-Dec-11 10:49
memberMassimo Conti12-Dec-11 10:49 
QuestionChange .wmv to .avi Pin
Rdechaux12-Nov-11 3:09
memberRdechaux12-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
memberwarefcool2-Nov-11 14:38 
Questioncould you please convert this wonderful code in VB.NET Pin
swaby23-Oct-11 16:05
memberswaby23-Oct-11 16:05 
AnswerRe: could you please convert this wonderful code in VB.NET Pin
Massimo Conti25-Oct-11 9:39
memberMassimo Conti25-Oct-11 9:39 
GeneralRe: could you please convert this wonderful code in VB.NET Pin
swaby25-Oct-11 12:02
memberswaby25-Oct-11 12:02 
QuestionOverlay? Pin
Kawasaki_KMX7-Sep-11 3:14
memberKawasaki_KMX7-Sep-11 3:14 
GeneralMy vote of 5 Pin
Bruce Lee Harrison31-Aug-11 5:07
memberBruce Lee Harrison31-Aug-11 5:07 
QuestionGrab image Pin
zevuv26-Aug-11 2:30
memberzevuv26-Aug-11 2:30 
AnswerRe: Grab image Pin
Massimo Conti27-Aug-11 9:44
memberMassimo Conti27-Aug-11 9:44 
GeneralMy vote of 5 Pin
itsho21-Jul-11 10:42
memberitsho21-Jul-11 10:42 
GeneralMy vote of 5 Pin
descartes12-Jul-11 11:53
memberdescartes12-Jul-11 11:53 
QuestionCompiler VS 2008...? Pin
descartes11-Jul-11 11:37
memberdescartes11-Jul-11 11:37 
GeneralMy vote of 5 Pin
ivix4u5-Jul-11 0:10
memberivix4u5-Jul-11 0:10 
QuestionMy vote of 5 Pin
Filip D'haene30-Jun-11 5:01
memberFilip D'haene30-Jun-11 5:01 
QuestionChanging Video Size Pin
NYCChris21-Jun-11 7:33
memberNYCChris21-Jun-11 7:33 
AnswerRe: Changing Video Size Pin
Massimo Conti21-Jun-11 10:04
memberMassimo Conti21-Jun-11 10:04 
Generalgetting error upon selecting "Start Recording" Pin
acheoacheo14-Jun-11 13:58
memberacheoacheo14-Jun-11 13:58 
GeneralRe: getting error upon selecting "Start Recording" Pin
Massimo Conti15-Jun-11 8:56
memberMassimo Conti15-Jun-11 8:56 
GeneralI can't wait to use this! :) Pin
Anthony Daly12-Jun-11 12:23
memberAnthony Daly12-Jun-11 12:23 
Generalgreat article [modified] Pin
twinscythe123327-Jun-11 22:27
membertwinscythe123327-Jun-11 22:27 
GeneralRe: great article Pin
Massimo Conti8-Jun-11 19:27
memberMassimo Conti8-Jun-11 19:27 
GeneralRe: great article Pin
twinscythe123328-Jun-11 20:03
membertwinscythe123328-Jun-11 20:03 
GeneralRe: great article Pin
Massimo Conti12-Jun-11 10:33
memberMassimo Conti12-Jun-11 10:33 
GeneralRe: great article Pin
twinscythe1233212-Jun-11 20:21
membertwinscythe1233212-Jun-11 20:21 

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
Web02 | 2.8.190526.1 | Last Updated 4 Jul 2011
Article Copyright 2011 by Massimo Conti
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid