【问题标题】:Stretch the video to the entire browser将视频拉伸到整个浏览器
【发布时间】:2020-05-07 22:06:53
【问题描述】:

请帮我将视频拉伸到 Windows 窗体中的完整网络浏览器。 我使用的代码:

web_browser.DocumentText = String.Format("<html><head>" +
  "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=Edge\"/>" +
  "</head><body>" +
  "<iframe width=\"100%\" height=\"218\" src=\"https://www.youtube.com/embed/{0}\"" +
  "frameborder = \"0\" allow = \"autoplay; encrypted-media\" allowfullscreen></iframe>" +
  "</body></html>", VideoID);

【问题讨论】:

  • 白条是视频本身的一部分吗?

标签: c# html winforms webbrowser-control


【解决方案1】:

这没有意义?我假设这是一个 youtube 视频?

您可以从任何 youtube 视频中获取嵌入链接,例如 &lt;iframe width="560" height="315" src="https://www.youtube.com/embed/BsyJMy8_lO0" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen&gt;&lt;/iframe&gt;

并将其嵌入到 jsx 或 html 中?然后更改宽度和高度属性

【讨论】:

  • 我问了一个具体问题,我需要去掉这些白色条纹,我根本不想看到它们,你能帮我解决这个问题吗?我问了一个具体问题,我需要去掉这些白色条纹,我根本不想看到它们,你能帮我吗?
【解决方案2】:

您可以使用&lt;style type="text/css"&gt;HEAD 块内设置内联样式,在设置IFrame width 时影响bodyhtml 元素sizemarginpaddingheight100% 作为&lt;iframe&gt; 元素本身的属性。

视频大小成比例。如果您需要将视频限制为特定大小,您始终可以设置 WebBrowser.MaximumSize 属性(或其容器表单的相同属性,如果 WebBrowser 被锚定/停靠)。

请注意,我使用的是插值字符串,但当然使用 string.Format() 也是一样的。

string videoID = "Some ID";

webBrowser1.Navigate("");
var doc = webBrowser1.Document.OpenNew(true);
doc.Write(
    "<html><head><meta http-equiv=\"X-UA-Compatible\" content=\"IE=Edge\"/> " +
    "<style type=\"text/css\"> " +
    "body { background: black; margin: 0px; padding: 0px; border: 0px; width: 100%; height: 100%; }" +
    "iframe { margin: 0px; padding: 0px; border: 0px; display: block; }" +
    "</style></head><body> " +
    $"<iframe width=\"100%\" height=\"100%\" src=\"https://www.youtube.com/embed/{videoID} \"" +
    "allow = \"autoplay; encrypted-media\" allowfullscreen></iframe>" +
    "</body></html>");

webBrowser1.Refresh();

background: black; 无关紧要。只是视频播放器通常的背景颜色。

激活 WebBrowser 高级兼容模式和 GPU 渲染支持

在项目中添加WebBrowserAdvancedFeatures类,并在Form的构造函数中插入这个函数调用,需要在WebBrowser初始化之前激活这些功能。
Form.OnFormClosing 中,高级功能被禁用。

using System.Security.AccessControl;
using Microsoft.Win32;

public SomeForm()
{
    InitializeComponent();
    WebBrowserAdvancedFeatures.ActivateWBAdvancedFeatures(Path.GetFileName(Application.ExecutablePath));
}

protected override void OnFormClosing(FormClosingEventArgs e)
{
    WebBrowserAdvancedFeatures.DeactivateWBAdvancedFeatures(Path.GetFileName(Application.ExecutablePath));
    base.OnFormClosing(e);
}

// (...)

class WebBrowserAdvancedFeatures
{
    private static string baseKeyName = @"Software\Microsoft\Internet Explorer\Main\FeatureControl";
    private static string featuresKey = baseKeyName+ @"\FEATURE_BROWSER_EMULATION";
    private static string hardwareAccelKey = baseKeyName + @"\FEATURE_GPU_RENDERING";

    public static void ActivateWBAdvancedFeatures(string executableName)
    {
        RegistryKey wbFeatureKey = null;
        RegistryKey wbGpuKey = null;
        try {
            wbFeatureKey = Registry.CurrentUser.OpenSubKey(featuresKey,
            RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.WriteKey);

            if (wbFeatureKey == null) {
                wbFeatureKey = Registry.CurrentUser.CreateSubKey(featuresKey, true);
            }
            wbFeatureKey.SetValue(executableName, 11001, RegistryValueKind.DWord);

            wbGpuKey = Registry.CurrentUser.OpenSubKey(hardwareAccelKey,
                RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.WriteKey);

            if (wbGpuKey == null) {
                wbGpuKey = Registry.CurrentUser.CreateSubKey(hardwareAccelKey, true);
            }
            wbGpuKey.SetValue(executableName, 11001, RegistryValueKind.DWord);
        }
        finally {
            wbFeatureKey?.Dispose();
            wbGpuKey?.Dispose();
        }
    }
    public static void DeactivateWBAdvancedFeatures(string executableName)
    {
        using (var wbFeatureKey = Registry.CurrentUser.OpenSubKey(featuresKey,
            RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.WriteKey)) {
            wbFeatureKey.DeleteValue(executableName, false);
        }

        using (var wbAccelKey = Registry.CurrentUser.OpenSubKey(hardwareAccelKey,
            RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.WriteKey)) {
            wbAccelKey.DeleteValue(executableName, false);
        }
    }
}

【讨论】:

  • 在我使用了你的解决方案之后,情况变得更糟了。imgur.com/WFWoUBd
  • 我做了一个编辑,应该使它更兼容(复制所有内容,不要更改现有代码)。另外,您是否启用了advanced features
  • See the result I have 使用这些设置。
  • imgur.com/a/drfhUcZ 我不明白你是怎么得到这个结果的,但这是我的
  • 你没有用我在这里发布的内容更正代码。另外,您是否启用了我之前评论中描述的高级功能?反正先更新代码,你现在用的不行了。
猜你喜欢
  • 1970-01-01
  • 2018-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多