将视频扩展到整个浏览器 [英] Stretch the video to the entire browser
问题描述
请帮助我将视频扩展到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屋!