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.
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).
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.
Activation of the WebBrowser advanced compatibility mode and GPU rendering support
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);
}
}
}