将视频扩展到整个浏览器 [英] Stretch the video to the entire browser

查看:40
本文介绍了将视频扩展到整个浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我将视频扩展到Windows窗体中的完整网络浏览器.我使用的代码:

Please help me stretch the video to the full web browser in Windows Forms. The code I use:

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);

推荐答案

您可以在 HEAD 块中使用< style type ="text/css";> ,从而影响 body html 元素 size margin padding,同时将IFrame的 width height 设置为 100%作为< iframe> 的属性元素本身.

You can set an in-line style inside the HEAD block with <style type="text/css">, affecting the body and html elements size, margin and padding while setting the IFrame width and height to 100% as attributes of the <iframe> element itself.

视频大小是成比例的.如果您需要将视频限制为特定大小,则始终可以设置 WebBrowser.MaximumSize 属性(如果已锚定WebBrowser,则可以设置其容器Form的相同属性)/docked).

The video size is proportional. If you need to constraint the video to a specific size, you can always set the WebBrowser.MaximumSize property (or the same property of its container Form, if the WebBrowser is anchored/docked).

请注意,我使用了插值字符串,但是使用 string.Format()当然是相同的.

Note that I have used an interpolated string, but it's of course the same using 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; is irrelevant. Just the usual background color of Video players.

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

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

Add the WebBrowserAdvancedFeatures class to the Project and insert this function call in the Form's constructor, the features need to be activated before the WebBrowser is initialized.
In Form.OnFormClosing, the advanced features are disable.

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);
        }
    }
}

这篇关于将视频扩展到整个浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆