C# 在 WinForms 应用程序中嵌入视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/332117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:33:26  来源:igfitidea点击:

Embedding Video in a WinForms app

c#winformsvideo

提问by Jason

I need to be able to embed and control the playback of an AVI file in a WinForms app, using C#. The video needs to be embedded in the form, not launched in a separate media player window.

我需要能够使用 C# 在 WinForms 应用程序中嵌入和控制 AVI 文件的播放。视频需要嵌入到表单中,而不是在单独的媒体播放器窗口中启动。

What's the best approach to do this? I found the System.Media namespace, which sounded promising, but it appears that is only useful for sound.

这样做的最佳方法是什么?我找到了 System.Media 命名空间,听起来很有希望,但它似乎只对声音有用。

Do I use DirectX to do this? MCI? Or some other approach?

我是否使用 DirectX 来执行此操作?MCI?或者其他一些方法?

回答by Patrick Desjardins

You can use Media Playerinside your Winform. This would been an easy way to do it.

您可以在 Winform 中使用媒体播放器。这将是一个简单的方法来做到这一点。

回答by Brian Genisio

I would considerusing the WPF media controls and just use the ElementHost to put your WPF control inside your WinForms app. I think you will get a much more rich experience.

我会考虑使用 WPF 媒体控件,只需使用 ElementHost 将 WPF 控件放在 WinForms 应用程序中。我想你会得到更丰富的经验。

See System.Windows.Forms.Integrationfor more information

有关详细信息,请参阅System.Windows.Forms.Integration

回答by Guge

The suggestions from Daokand Brian Genisioare both good options. Let me add a third: DirectShow. Used to be part of DirectX but has now been promoted to the Windows SDK. There are many good C# sample applications to look at, and it gives complete control of the playback.

DaokBrian Genisio的建议都是不错的选择。让我添加第三个:DirectShow。曾经是 DirectX 的一部分,但现在已升级为 Windows SDK。有许多优秀的 C# 示例应用程序可供查看,它可以完全控制播放。

回答by bvanderw

I highly recommend this library:

我强烈推荐这个库:

http://directshownet.sourceforge.net/

http://directshownet.sourceforge.net/

It is a .NET wrapper around the DirectShow API.

它是一个围绕 DirectShow API 的 .NET 包装器。

(The sample apps should get you going very quickly.)

(示例应用程序应该可以让您快速上手。)

--Bruce

——布鲁斯

回答by Andres A.

The way i did it was, and I quote:

我这样做的方式是,我引用:

Right-click the Toolbox, and click Choose Items.

Note: If you do not see the Toolbox, click Toolbox on the View menu to open it. The Customize Toolbox Items dialog box opens.

On the COM Components tab, select the Windows Media Player check box, and then click OK.

The Windows Media Player control appears on the current Toolbox tab.

When you add the Windows Media Player control to the Toolbox, Visual Studio automatically adds references to two libraries: AxWMPLib and WMPLib. The next step is to add the control to the Windows Form.

To add the Windows Media Player control to a Windows Form

Drag the Windows Media Player control from the Toolbox to the Windows Form.

In the Properties window, set the Dock property to Fill. You can do this by clicking the center square.

Double-click the title bar of the form to add the default Load event in the Code Editor.

Add the following code to the Form_Load event handler to load a video when the application opens.

右键单击工具箱,然后单击选择项目。

注意:如果您没有看到工具箱,请单击“视图”菜单上的“工具箱”将其打开。“自定义工具箱项目”对话框打开。

在 COM 组件选项卡上,选中 Windows Media Player 复选框,然后单击确定。

Windows Media Player 控件出现在当前的工具箱选项卡上。

将 Windows Media Player 控件添加到工具箱时,Visual Studio 会自动添加对两个库的引用:AxWMPLib 和 WMPLib。下一步是将控件添加到 Windows 窗体。

将 Windows Media Player 控件添加到 Windows 窗体

将 Windows Media Player 控件从工具箱拖动到 Windows 窗体。

在“属性”窗口中,将 Dock 属性设置为 Fill。您可以通过单击中心方块来执行此操作。

双击窗体的标题栏以在代码编辑器中添加默认的 Load 事件。

将以下代码添加到 Form_Load 事件处理程序以在应用程序打开时加载视频。

axWindowsMediaPlayer1.URL = 
    @"http://go.microsoft.com/fwlink/?LinkId=95772";

http://msdn.microsoft.com/en-us/library/bb383953(v=vs.90).aspx

http://msdn.microsoft.com/en-us/library/bb383953(v=vs.90).aspx