在 C# 中使用网络摄像头,搭配 .NET Framework 4.0 和 Microsoft Expression Encoder 4






4.86/5 (75投票s)
如何在 C# 中使用网络摄像头。
引言
如果你想以一种简单的方式在 C# 中使用你的网络摄像头,那么这篇文章就是为你准备的。为了实现我们的目标,我们需要 Microsoft .NET 4.0 和 Microsoft Expression Encoder 4。你可以通过“Microsoft Web Platform Installer”**免费**获取后者,可以从这里下载:http://www.microsoft.com/web/downloads/platform.aspx。
之后,我们需要创建一个 Windows Forms 应用程序,并向项目添加以下引用
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
以下是枚举视频和音频设备的示例代码
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);
}
以下是预览视频和音频的示例代码
// 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);
以下是将视频和音频录制到 .wmv 文件的示例代码
// 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();
通过网络流式传输网络摄像头
以下是将你的网络摄像头的视频(和音频)通过网络进行流式传输的示例代码
// 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();
要查看广播,你可以创建一个 WPF 应用程序并使用 MediaElement
。只需要一行代码!以下是整个 WPF 应用程序的代码
<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="https://:8080" />
</Grid>
</Window>
结论
轻而易举,不是吗?这是管理 C# 中的视频和音频的新前沿。在 C# 的早期,使用网络摄像头并不容易。我希望这能帮助那些想玩转网络摄像头的人。感谢阅读我的第一篇文章 :-)