I am building a C# application with a WebBrowser
and I am trying to figure out a way to block images, i.e. for them to to not display when a website is loading (so that the website loads more easily).
I've tried to remove the <img>
tags by getting it via webBrowser1.DocumentText
and using Regex.Replace
to remove the images, but then it shows me a blank page with aaa
when I'm using the code. Is there a better way to remove the images? Any help greatly appreciated.
Code:
var html_source = webBrowser1.DocumentText;
var newOne = Regex.Replace(html_source, "<img.*/>", "", RegexOptions.Multiline);
webBrowser1.DocumentText = newOne + "aaa";
Update:
I have tried below code (just for testing) but is still shows me just aaa
.
var html_source = webBrowser1.DocumentText;
webBrowser1.DocumentText = html_source + "aaa";
Scroll down to Privacy and click Content Settings. A pop-up menu will appear, scroll down to Images and click Manage Exceptions. Next is the image exceptions page. On this page, type in a web address, select whether to allow or block images and then press Enter to save it to the list.
You can get there from the three-dotted menu in the right hand corner of your browser, or by typing chrome://settings in the URL bar. Scroll down and click on Advanced . Click on Content Settings from 'Privacy and security'. Select Images from there.
To prevent images from your site appearing in Google's search results, add a robots. txt file to the root of the server that blocks the image.
EDIT
Found this question on SO and a complete project which may help you on codeproject.com. Within this example there is a userControl using the webBrowser COM component. As I wrote in my original answer I don't think it is possible to prevent the .net Framework WebBrowser to load images. You need access the level below to intercept loading images after the browser control has received the plain html text.
... The most obscure and important part of the control is the IDispatch_Invoke_Handler(). ... how to implement IDispatch::Invoke in order to restrict what IE was showing (such as images, ActiveX controls, Java). I found out that if you add a IDispatch_Invoke_Handler() method in your code with the COM dispatch identifier of -5512, this does the job for you. A very obscure answer, but it works well....
ORIGINAL
You may try this
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
Debug.WriteLine("documentCompleted");
HtmlDocument doc = webBrowser1.Document;
foreach (HtmlElement imgElemt in doc.Images)
{
imgElemt.SetAttribute("src", "");
}
}
But as MSDN says
Handle the DocumentCompleted event to receive notification when the new document finishes loading. When the DocumentCompleted event occurs, the new document is fully loaded, which means you can access its contents through the Document, DocumentText, or DocumentStream property.
I don't think you can do this with the webBrowser control from the .net Framework.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With