How can you block the image to appear on WebBrowser Control? - c#

I have created a simple web browser using c# and I want to block the images to appear on WebBrowser control. Can you please show me a sample code for this? Thanks in advance.

You can remove image-tags from the source htm like this:
string content = new WebClient().DownloadString(#"http://www.google.com/");
string contentWithoutImages = Regex.Replace(content, #"<img(.|\n)*?>", string.Empty);
webBrowser1.DocumentText = contentWithoutImages;

Related

C# write a string to a variable defined in %name% in a loaded html page in WebBrowser control

I loaded a html local file in a WebBrowser control within a WinForm. In the html code, I defined some variables in %variables%.
My question is how to transfer/reference strings/data from WinForm to the %viarables% defined in the loaded html page, and refresh the loaded html page displayed on the WebBrowser.
Following is the code of loading a local html file from. Any suggestion will be appreciated.
By the way, it is a WinForm application, not asp.Net.
string curDir = Directory.GetCurrentDirectory();
this.webBrowser1.Url = new Uri(String.Format("file:///{0}/{1}", curDir + "\\Forms", fileName));
Instead of trying to replace your variables after loading the HTML, which could be problematic, why not read the source file, replace the variables, then load the updated document into the web browser control? This seems much more straightforward.
For example:
string dir = Path.Combine(Directory.GetCurrentDirectory(), "forms");
string html = File.ReadAllText(Path.Combine(dir, fileName));
foreach (string variable in GetListOfVariables())
{
html = html.Replace(variable, GetReplacementForVariable(variable));
}
webBrowser1.DocumentText = html;
Do you have control over contents of the HTML file? You could try InvokeScript method paired with a Javascript function prepared in the document beforehand.
If you don't control the contents, then you can inject javascript from your code. Here are some examples of doing that.

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 Print File in wpf?

How to print a specified file in WPF using PrintDialog.PrintDocument() method?
I want to Print the html page using print Dialog.
You'll need some kind of an html renderer in order to print it. either IEExplorer (look into shdocvw.dll and mshtml.dll) or Awesomium etc...
good luck.
It is possible by using the Print() method of the WebBrowser Class in C# to do this. Just put the content to the WebBrowser Control like so:
webBrowser1.DocumentContent = openfiledialog.FileName;
Then just call the "webBrowser1.Print();" method.
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.print.aspx
Refer MIcrosft.mshtml com object
mshtml.IHTMLDocument2 doc = rptPreview.Document as mshtml.IHTMLDocument2; doc.execCommand("Print", true, null);

How to show html formatted content (without image) in a winform?

I want to show html formatted string in my winform application. What control should I use?
Use WebBrowser control to display html content in WinForms applications.
You can specify just html content:
Dim html As string = "<span>my html content</span>"
webBrowser.DocumentText = html
or specify path to the html content:
webBrowserNotes.Url = "my-html-content.html"
If you just want "simple" HTML formatting (e.g. underline, bold, text color, etc) you could use this custom control from Oscar Londono on code project: http://www.codeproject.com/KB/edit/htmlrichtextbox.aspx
One option could be to use WebBrowser Control. Have a look at this link.
The webbrowser control. You can find it under common controls.
Using the WebBrowser Control would be a good option. But if you want to use HTML5 you would be better of looking at .NET web browser libraries such as GeckoFX

Find text in webbrowser control

I have a web browser control, already navigated to a page. Now I want to search for a specific text in the page, and scroll to the first occurrence.
Being able to scroll to other occurrences is a bonus.
You can try this code:
webBrowser1.Select();
SendKeys.Send("^f");
I don't know if it works in a WebBroswer. We make the broswer(IE/FF/etc) window scroll to some text with the following code:
//source code of http://www.sample.com/sample.html
<html>
...
<span name="aim">KeyWord</span>
...
</html>
If I want the window to scroll to the "KeyWord", simply visit http://www.sample.com/sample.html#aim
Using string document = myWebBrowser.DocumentText to get the source code of the page, and search the text in the string, get its node name, and navigate it using #.
See this if it helps:
string PageSource = null;
PageSource = WebBrowser1.Document.Body.InnerHtml();
if (Strings.InStr(PageSource, stringtoFind) > 0) {
...insert an Anchor tag here and then use
WebBrowser1.Navigate to go to the the new URL with the #Anchor tag
} else {
...whatever else
}
One way...
Use the Ctrl + F Key to invoke Find, native to the WebBrowser Control?

Categories