'How can I embed <iframe> HTML code into C# application?

how can I embed a HTML iframe into my c# application most efficiently.

Let's say this youtube video:

<iframe width="560" height="315" src="//www.youtube.com/embed/UJ53Js0YKZM" frameborder="0" allowfullscreen></iframe>

I have tried this one but did not work:

Private Sub InitializeComponent()
    Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(frmAbout))
    '
    'frmVirtual
    '
    Me.Text = "Virtual"
    Me.VirtualView = New WebBrowser
    Me.VirtualView.Navigate("<iframe src='https://www.google.com/maps/embed?pb=!1m0!3m2!1sen!2sau!4v1481252003737!6m8!1m7!1sF%3A-pwYGx-oWTIk%2FWC6LeJuIxdI%2FAAAAAAAABIg%2FphppDvMZr54JiWnLbsbUgDcTGUfGXLMRACLIB!2m2!1d-33.76525136331761!2d150.9088391438127!3f310!4f0!5f0.7820865974627469' width='600' height='450' frameborder='0' style='border: 0' allowfullscreen></iframe>")

   Me.SuspendLayout()
End Sub

The C# application would be a Windows Form Application of course. It doesn't necessarly has to be a video, but even just a small banner with link, in an Iframe of course. Please help!



Solution 1:[1]

You can use the WebBrowser control accomplish this.

WebBrowser1.Navigate("https://www.youtube.com/embed/UJ53Js0YKZM?autoplay=1");

Solution 2:[2]

You cannot use an IFrame in a windows form application. There is however a windows form control named System.Windows.Forms.WebBrowser();

This can be used by navigating to toolbox (whilst on a win forms designer) and dragging on a web browser (Common Controls).

In the Form's class you need something like the following.

public Form1()
    {
        InitializeComponent();
        webBrowser1.Navigate("www.youtube.com/embed/UJ53Js0YKZM");
    }

If you really want a iframe the following may work

    private void Form1_Load(object sender, EventArgs e)
    {
        var page = @"
        <html>
            <body>
                <iframe width='560' height='315'
                    src='https//www.youtube.com/embed/UJ53Js0YKZM' frameborder='0'>
                </iframe>
            </body>
        <html>";
        webBrowser1.DocumentText = page;
        webBrowser1.ScriptErrorsSuppressed = true;
    }

Solution 3:[3]

If you only want to access the one youtube video, you can save that iframe code inside an otherwise empty html document and point the webBrowser to that.

string applicationDirectory = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
string myFile = System.IO.Path.Combine(applicationDirectory, "iFrame.html");

webBrowser1.Url = new Uri("file:///" + myFile);

If you want to have a more robust approach, you can build the iframe's html and send that as a string to the webBrowser's document. See How to display the string html contents into webbrowser control? for information on how to do that.

Solution 4:[4]

I have Windows 10 and a C#, UWP app. If you want to just show the contents (URL) of an iFrame, this worked for me. There was no need to write code in my xaml.cs file, since I only wanted to show a website.

<Grid>
    <WebView Source ="https://yourwebsite.com" Height="250" Width="650" />
</Grid>

The original iFrame was this:

<iframe style="height:250px; width:650px" src="https://yourwebsite.com></iframe>

For a YouTube video:

<Grid>
    <WebView Source ="https://www.youtube.com/embed/jJKEkZL9qrM" Height="250" Width="650" />
</Grid>

Solution 5:[5]

<iframe visible="true" runat="server" id="yourid" style="visibility:hidden;display:block;width:1px;height:1px;"></iframe>


yourid.Attributes.Add("src", "youtube.com");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Owain van Brakel
Solution 2
Solution 3 Community
Solution 4 Azurespot
Solution 5 Machavity