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

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!

You can use the WebBrowser control accomplish this.
WebBrowser1.Navigate("https://www.youtube.com/embed/UJ53Js0YKZM?autoplay=1");

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

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.

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>

<iframe visible="true" runat="server" id="yourid" style="visibility:hidden;display:block;width:1px;height:1px;"></iframe>
yourid.Attributes.Add("src", "youtube.com");

Related

audio record on web application using C# [duplicate]

I want to play some sounds in my web page once I click a button. This is my code but it shows an error.
SoundPlayer x = new SoundPlayer();
x.SoundLocation = "WindowsBalloon.wav";
//x.Play();
x.PlaySync();
error:
Please be sure a sound file exists at the specified location.
but the file exists in my project and I'm sure that the address is correct.
You cannot play a file on a web page using the System.Media.Soundplayer class !!!
Reason
It will play sound on server-side not client-side.
As mentioned as in below links
- Problem With The C# System.Media.SoundPlayer Class On A Web Host
- What is the most “compatible” way of autoplaying sound ?
Solution
Other SO Answer over this same requirements.
Use Any other Flash or Silverlight based plugins.
Use html embed tag or html5 audio tag. Examples can be seen on w3schools
Html5-based audio solutions (works on modern browsers only)
<embed> tag: The <embed> tag defines a container for external (non-HTML) content. (It is an HTML5 tag, invalid in HTML 4, but works in all browsers).
<embed height="100" width="100" src="horse.mp3" />
<object> tag: The <object> tag can also define a container for external (non-HTML) content.
<object height="100" width="100" data="horse.mp3"></object>
<audio> tag: The <audio> element is an HTML5 element, invalid in HTML 4, but it works in all browsers.
<audio controls="controls" height="100" width="100">
<source src="horse.mp3" type="audio/mp3" />
<source src="horse.ogg" type="audio/ogg" />
<embed height="100" width="100" src="horse.mp3" />
</audio>
Please note the problems with html5-based solutions you must convert your videos to different formats.
- The <audio> element does not validate as HTML 4 and XHTML.
- The <embed> element does not validate as HTML 4 and XHTML.
- The <embed> element cannot "fall-back" to display an error.
You need to use <object> or <embed> html tags.
<object data="WindowsBalloon.wav"></object>
Or HTML5 tag
<audio src="WindowsBalloon.wav">
<p>Your browser does not support the audio element.</p>
</audio>
This works in HTML5 :
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("<embed height='0' width='0' src='Sound.wav' />");
}
This is what I think you want:
Server.MapPath(string path);
Returns the physical file path that corresponds to the specified virtual path on the Web server.
Parameters: path: The virtual path of the Web server.
Returns: The physical file path that corresponds to path.
SoundPlayer s = new SoundPlayer();<br>
s.SoundLocation = **Server.MapPath("WindowsBalloon.wav");**<br>
s.PlaySync();
Given full path i.e. c:\wavfiles\WindowsBalloon.wav
'wavfiles' above is a user privileged folder.
use x.PlayLooping()
function if you want to play sound file continuously
BE CAREFUL!
use one button to exit loop else sound file will run continuously. I suggest you to exit the loop: -
Code
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
x.Stop()
End Sub
If you need to play an ALARM sound programmatically you can do it this way:
<asp:Panel runat="server" ID="panBuzz" style="visibility:hidden">
<audio runat="server" id="Buzz" src="http://.....mp3" type="audio/mp3"/>
</asp:Panel>
Code behind (visual basic):
Dim cBuzz As HtmlControl = DirectCast(panBuzz.FindControl("Buzz"), HtmlControl)
cBuzz.Attributes.Add("autoplay", "autoplay")
Code behind (C#):
HtmlControl cBuzz = (HtmlControl)panBuzz.FindControl("Buzz");
cBuzz.Attributes.Add("autoplay", "autoplay");
try adding the drive letter to the path, such as "C:/WindowsBalloon.wav". But this would not play it on the client side. I would recomend trying HTML5 for the client side.
SoundPlayer s = new SoundPlayer();
s.SoundLocation = Server.MapPath("WindowsBalloon.wav");
s.PlaySync();

How can I show text with html format in xamarin forms

I work on webservice with json and i get text with html format. I want my text have hyperlinks and some other properties where i find from html tags (etc. bold).
I try binding my html string in WebView source but WebView is every time blank. I use this code
var browser = new WebView();
var htmlSource = new HTMLWebViewSource();
htmlSource.Html = MyItem.Article;
browser.Source = htmlSource;
MyItem.Article string is like this
I want something like this inside Label where is inside ListView os something like that.
How can I do this?
This should work for you
string htmlText = MyItem.Article.ToString().Replace(#"\", string.Empty);
var browser = new WebView ();
var html = new HtmlWebViewSource {
Html = htmlText
};
browser.Source = html;
Because Xamarin.Forms.HtmlWebViewSource.HTML expect a pure HTML. Using this you can create a Xamarin.Forms user control with the help of this article http://blog.falafel.com/creating-reusable-xaml-user-controls-xamarin-forms/ Cheers..!
In XAML you can do something like this:
<WebView>
<WebView.Source>
<HtmlWebViewSource Html="{Binding HtmlText}"/>
</WebView.Source>
</WebView>
You may also need to provide Height and Width of the WebView if it's not inside a Grid.
FYI, I've just added the ability to my Forms9Patch library to create labels and buttons where you can format the text via HTML. For example:
new Forms9Patch.Label { HtmlText = "plain <b><i>Bold+Italic</i></b> plain"}
... would give you a label where the text has been formatted bold italic in the middle of the string.
Also, as an aside, it allows you to use custom fonts that are embedded resources in your PCL project without any platform specific work. And, you can use these fonts via the HTLM <font> tag or and HTML font-family attribute.
Here are some screen shots from the demo app:
<pre>
<WebView VerticalOptions="FillAndExpand">
<WebView.Source>
<HtmlWebViewSource Html="{Binding HtmlText}"/>
</WebView.Source>
</WebView>
</pre>

How to set width of the text in System.Windows.Forms.WebBrowser?

I am currently using the control System.Windows.Forms.WebBrowser to create an Wysiwyg editor in a windows application.
Basically what I need is that the text does not exceed the width of the control, ie I do not want to use a horizonal scrollbar.
Is it possible?
[UPDATE]
this is the code for the moment I am running:
Reconocimiento.Navigate("about:blank"); //this is my object System.Windows.Forms.WebBrowser
Reconocimiento.Document.OpenNew(false);
string html = "<html><head><style type=\"text/css\">body {width: 400px;}</style></head><body></body></html>";
Reconocimiento.Document.Write(html);
recon = (Reconocimiento.Document.DomDocument) as IHTMLDocument2;
recon.designMode = "On"; //What we just did was make our web browser editable!
You can make use of the css word-break property.
Create a file with the following contents and save it to say C:\default.html (or some application path)
<html>
<body style='word-break: break-all'>
<body>
</html>
In you code, instead of navigating to about:blank, call
Reconocimiento.Navigate(new Uri("file:///C:/default.html"));

Send text to textarea in webbrowser

i have a webbrowser component in a windows application.
could anybody please tell me how to send text to textboxes in webpages appearing in this browser programatically?
my HTML code
<textarea name="message" id="vB_Editor_QR_textarea" rows="10" cols="60" style="width:100%; height:100px" tabindex="1" dir="ltr"></textarea>
Edit:
Edit: Can you post your code?? I tried and it worked.
I added a WebBrowser control and during Form Load I set the HTML
webBrowser1.DocumentText = "<textarea name='message' id='t' rows='10' cols='60' style='width:100%; height:100px' tabindex='1' dir='ltr'></textarea>";
Added a button and added the following code for Button Click event
HtmlElement el = webBrowser1.Document.All["t"];
el.InnerText = "Hello World";
and it works.
WebBrowser has Document property using which you can achieve your requirements. For example to click a button you can use the following code
HtmlElement el = webBrowser1.Document.All["btnI"];
if (el != null) el.InvokeMember("click");
Sample code is copied from here
WebBrowser Class documentation
Hope this serves as a starting point. Try it and If you have any specific issues. Update your question and we will help.

How can I preview html file in another window?

Hy,
In my application I store a html code in a string, for example:
string myHtml = "<html><body><input type ='text' value='hello'/></body></html>";
How can I preview this html in another window pressing some button?
<asp:Button ID="PreviewButton" runat="server" Text="Preview" OnClick="PreviewButton_Click"/>
I've tried :
protected void PreviewButton_Click(object sender, EventArgs e)
{
myHtml = "<html><body><input type ='text' value='hello'/></body></html>";
Response.Write(myHtml);
Response.End();
}
And it works, the preview it's opened but in the same window.. does anyone know how can I open it in another window?
Thanks in advance.
Jeff
You could store the source in a javascript string and use window.open and then write to the new window the source.
Remeber to escape the source correctly first.
You can call window.open in Javascript to open a new window showing an ASHX handler that serves your HTML. (you may need to pass information on the query string)
Remember to set the Response.ContentType in the handler.

Categories