Click here to Skip to main content
11,478,902 members (59,240 online)
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 238K 24.8K 167
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 Smile | :)

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

Comments and Discussions

 
QuestionHow can I access the video from a web page? Pin
Member 113118281-Mar-15 13:01
memberMember 113118281-Mar-15 13:01 
QuestionSystem.Deployment.Application.InvalidDeploymentException Pin
Jirka Šáda2-Feb-15 6:18
memberJirka Šáda2-Feb-15 6:18 
QuestionScreen Capture Pin
fazilamehtab13-Oct-14 23:24
memberfazilamehtab13-Oct-14 23:24 
AnswerRe: Screen Capture Pin
Mehdi Rostami22-Oct-14 9:30
memberMehdi Rostami22-Oct-14 9:30 
QuestionPlease Help me Pin
Member 110602839-Sep-14 7:22
memberMember 110602839-Sep-14 7:22 
QuestionHey there. Pin
Member 104321188-Jun-14 5:21
memberMember 104321188-Jun-14 5:21 
Questionstart stop recording button only makes one file which is latest recorded Pin
Member 801762717-Apr-14 21:55
memberMember 801762717-Apr-14 21:55 
AnswerRe: start stop recording button only makes one file which is latest recorded Pin
Member 111690743-Nov-14 23:03
memberMember 111690743-Nov-14 23:03 
Questionstart encoding Pin
badi2386-Apr-14 22:57
memberbadi2386-Apr-14 22:57 
AnswerRe: start encoding Pin
HugoAmi23-Sep-14 17:02
memberHugoAmi23-Sep-14 17:02 
Generalwebcam capture Pin
rakdosss2-Apr-14 6:52
memberrakdosss2-Apr-14 6:52 
QuestionStrong Delay Pin
erebos00717-Feb-14 13:42
membererebos00717-Feb-14 13:42 
AnswerRe: Strong Delay Pin
selfwalker24-Jun-14 0:33
memberselfwalker24-Jun-14 0:33 
QuestionCould not load file or assembly 'Microsoft.Expression.Encoder, Version=4.0.0.0, 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 23:11
memberIan Vic Damacio8-Feb-14 23:11 
SuggestionRe: Could not load file or assembly 'Microsoft.Expression.Encoder, Version=4.0.0.0, 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 8:41
professionalDiego G. Fritz31-Jul-14 8:41 
QuestionAccess is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) Pin
Ian Vic Damacio24-Jan-14 23:00
memberIan Vic Damacio24-Jan-14 23:00 
AnswerRe: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) Pin
Mehdi Rostami22-Oct-14 9:31
memberMehdi Rostami22-Oct-14 9:31 
QuestionOne very bad assumption regarding newer versions of Windows (8.0 and above...) in this sample program... Pin
Destiny7774-Nov-13 12:32
memberDestiny7774-Nov-13 12:32 
Questionencoder need to be installed on every System? Pin
fine_dost13-Jul-13 13:39
memberfine_dost13-Jul-13 13:39 
AnswerRe: encoder need to be installed on every System? Pin
Destiny77722-Nov-13 16:35
memberDestiny77722-Nov-13 16:35 
GeneralMy vote of 5 Pin
King Coffee9-Jul-13 14:51
memberKing Coffee9-Jul-13 14:51 
QuestionModify or add blank frames? Pin
tuliosouza4-Jun-13 11:05
membertuliosouza4-Jun-13 11:05 
AnswerRe: Modify or add blank frames? Pin
Destiny77722-Nov-13 16:41
memberDestiny77722-Nov-13 16:41 
QuestionHelp me. please! Pin
nguyễn ngọc vỹ27-May-13 19:27
membernguyễn ngọc vỹ27-May-13 19:27 
AnswerRe: Help me. please! Pin
Destiny77722-Nov-13 16:44
memberDestiny77722-Nov-13 16:44 
GeneralMy vote of 4 Pin
naseer baloch1-May-13 23:13
membernaseer baloch1-May-13 23:13 
QuestionNice Demo, but where is the audio when preview ? Pin
Johnson.Gao.12623-Apr-13 2:20
memberJohnson.Gao.12623-Apr-13 2:20 
Questionviewing stream on a remote computer Pin
Member 95262384-Apr-13 5:42
memberMember 95262384-Apr-13 5:42 
AnswerRe: viewing stream on a remote computer Pin
Member 1008220412-Jun-13 11:05
memberMember 1008220412-Jun-13 11:05 
QuestionWebcam is showing black image in preview Pin
Member 79864944-Apr-13 0:51
memberMember 79864944-Apr-13 0:51 
AnswerRe: Webcam is showing black image in preview Pin
Member 202563013-Jun-14 22:15
memberMember 202563013-Jun-14 22:15 
GeneralThanks! Pin
wcuthbertson8-Mar-13 10:52
memberwcuthbertson8-Mar-13 10:52 
QuestionI am getting black screen while previwing Pin
sudheendra pavan sundar8-Mar-13 2:53
membersudheendra pavan sundar8-Mar-13 2:53 
QuestionHelp Pin
makhondi4-Mar-13 7:22
membermakhondi4-Mar-13 7:22 
QuestionError on winfows 8 Pin
Tub-9520-Feb-13 5:10
memberTub-9520-Feb-13 5:10 
AnswerRe: Error on winfows 8 Pin
Member 44347192-Apr-14 9:40
memberMember 44347192-Apr-14 9:40 
GeneralMy vote of 5 Pin
Wanararbrio16-Jan-13 10:33
memberWanararbrio16-Jan-13 10:33 
GeneralMy vote of 5 Pin
XtErMiNaToR10211-Jan-13 0:56
memberXtErMiNaToR10211-Jan-13 0:56 
Questionusing Microsoft.Expression.Encoder.Utilities; not found help me Pin
Member 964439429-Dec-12 20:10
memberMember 964439429-Dec-12 20:10 
QuestionDoes not detect my usb Genius Camera Eye 312 Pin
Luis Zeta4-Dec-12 6:39
memberLuis Zeta4-Dec-12 6:39 
AnswerRe: Does not detect my usb Genius Camera Eye 312 Pin
Luis Zeta4-Dec-12 7:22
memberLuis Zeta4-Dec-12 7:22 
QuestionWebcam detector in c# Pin
Member 798053425-Nov-12 19:28
memberMember 798053425-Nov-12 19:28 
QuestionIP cam Pin
rodey vincent22-Nov-12 20:17
memberrodey vincent22-Nov-12 20:17 
QuestionAdjustement with wcf Pin
Andéol_0014-Nov-12 6:45
memberAndéol_0014-Nov-12 6:45 
GeneralMy vote of 4 Pin
Member 94807894-Oct-12 17:30
memberMember 94807894-Oct-12 17:30 
QuestionUnable to create Device Pin
harmurk20-Sep-12 4:15
memberharmurk20-Sep-12 4:15 
QuestionHow to implement in existing project Pin
Member 943991418-Sep-12 13:43
memberMember 943991418-Sep-12 13:43 
QuestionLive video display in c# .net Pin
Member 900485711-Jul-12 19:36
memberMember 900485711-Jul-12 19:36 
Questionincrustation Pin
gefkuz23-Jun-12 3:11
membergefkuz23-Jun-12 3:11 
Questionurgent please i need simple security camera application in c# or vb.net Pin
kaydyonis21-Jun-12 9:39
memberkaydyonis21-Jun-12 9:39 

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