0

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

enter image description here

Jimi
  • 29,621
  • 8
  • 43
  • 61

2 Answers2

1

This doesn't make sense? I'm assuming this is a youtube video?

You can grab the embed link from any youtube video like <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></iframe>

and embed that with jsx or html? Then change the width and height properties

jackstride
  • 53
  • 8
  • I asked a specific question, I need to remove these white stripes, I simply do not want to see them, can you help me with this?I asked a specific question, I need to remove these white stripes, I simply do not want to see them, can you help me with this? – Сергей Маерович Jan 21 '20 at 19:49
1

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);
        }
    }
}
Jimi
  • 29,621
  • 8
  • 43
  • 61
  • After I used your solution, it got even worse.https://imgur.com/WFWoUBd – Сергей Маерович Jan 22 '20 at 13:48
  • I have made an edit that should make it more *compatible* (copy everything over, don't make changes to existing code). Also, have you enabled the [advanced features](https://stackoverflow.com/a/38514446/7444103)? – Jimi Jan 22 '20 at 14:40
  • [See the result I have](https://imgur.com/rYOTp3R) with these settings. – Jimi Jan 22 '20 at 14:59
  • https://imgur.com/a/drfhUcZ I don't understand how you got this result, but here's my – Сергей Маерович Jan 22 '20 at 16:21
  • You didn't correct the code with what I posted here. Also, have you enabled the advanced features, as described in my previous comment? Anyway, first update the code, what you're using now won't work. – Jimi Jan 22 '20 at 16:32
  • Edited the code, and now a new problem. It is not possible to open the video in full screen, as well as black bars, which it would be desirable to get rid of https://imgur.com/a/QiRKVo5 – Сергей Маерович Jan 22 '20 at 16:32
  • Well, that's a completely different story. You cannot click on the overlay button and maximize your Form, that's another type container, unrelated with the WebBrowser. I've posted something like that for GeckoFx [here](https://stackoverflow.com/a/54508658/7444103). If you cannot make it work (you have to handle the HtmlDocument events yourself), post back. I'll see whether I can make it work with the standard WebBrowser. It appears, however, that the main problem is fixed now. I'll also post the implementation to set the advanced feature. – Jimi Jan 22 '20 at 16:37
  • complains about the code: using (var wbAccelKey = Registry.CurrentUser.OpenSubKey(hardwareAccelKey, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.WriteKey)) { wbAccelKey.SetValue(executableName, 1, RegistryValueKind.DWord); } – Сергей Маерович Jan 23 '20 at 10:37
  • See that your app is not set to `Prefer 32-bit` in `Project->Properties->Build` – Jimi Jan 23 '20 at 11:22
  • System.NullReferenceException: "An object reference does not indicate an instance of an object." – Сергей Маерович Jan 23 '20 at 12:08
  • Yes, well, what object is null? Did you read the previous comment? If you have an error of sort, you have to paste the text of the exception. Btw, that code works without any problem if you have set it up correctly and used it as it is. If something doesn't still work when you have followed the instructions, update your question with the code you're using now. – Jimi Jan 23 '20 at 12:09
  • Have you checked whether you app is set to to 32bit, as described? Have you registered the application executable, as shown in the second part of the code (all parts are needed)? – Jimi Jan 23 '20 at 12:15
  • Yes, I unchecked it, made a separate class for your code, and added it to the main form. – Сергей Маерович Jan 23 '20 at 12:17
  • So, it was 32bit. Now, all you have to do is to remove any changes you have made and copy/paste the code I posted here exactly as it is inside the Form that uses the WebBrowser control. Add the `WebBrowserAdvancedFeatures.ActivateWBAdvancedFeatures(...)` in the Form constructor, right after `InitializeComponent()`. The rest, you can just paste it in somewhere (inside the Form's code file). If you have exceptions, post the exceptions text. Update your question, showing what you have done. – Jimi Jan 23 '20 at 12:25
  • Your code just doesn't work, I don't understand what I should do with it? – Сергей Маерович Jan 31 '20 at 19:32
  • https://yadi.sk/d/shcfkYOrOMGt2Q Here is a test project that I did, but even there what you did doesn't work. Inside is a screenshot of the error – Сергей Маерович Jan 31 '20 at 19:56
  • I posted the project so you can see what I'm doing wrong, please help me – Сергей Маерович Feb 03 '20 at 14:48