How to block the images on web browser
Solution 1
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.
Solution 2
You may try this:
private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
{
if (webBrowser1.Document != null)
{
foreach (HtmlElement imgElemt in webBrowser1.Document.Images)
{
imgElemt.SetAttribute("src", "");
}
}
}
Solution 3
The webbrowser control uses the same settings that internet explorer uses.
you can easily disable images, but note that it will effect internet explorer, as well as your webbrowser control (and other programs that use internet explorer functionality)
to disable images from loading:
1.) open integent explorer
2.) go to 'tools' > 'internet options'
3.) go to the 'advanced' tab
4.) scroll down until you find the 'show pictures' checkbox, and uncheck it (it is in the 'multimedia' section)
the effects of this change are stored in the registry i beleive, so you should be able to edit it programatically as well. keep in mind that it will effect more than just your application, however.
Solution 4
Recently, I had a requirement to intercept and analyse ALL communication in a webbrowser control. I think the technique I used can help you.
What you need :
- Awesomium.Net: a Chromium engine based control for .net
- Fiddler Core : an http in-memory proxy, that allows you to monitor http communication.
- HtmlAgility pack : depending on the solution you choose, HAP can help you to dynamically change the DOM of html content, in a FAR MORE RELIABLE way than a regex.
I choose to use Awesomium because it provides a lot more features than the out of the box web browser control. In my case, it allows me to defines the proxy to use instead of the system-wide setting.
Fiddler Core is used to intercept communication. Its API provide ways to intercept/tamper/... when request are issued. In my case, I was only forwarding response bodies to my business classes, but in your case, you should be able to filter on mime-type to either change the HTML DOM (Use HtmlAgility pack !!!!!) or return non 200 http status for images.
Here is the code I used. My app is WPF, but you can adapt it to winform with few efforts :
public partial class App : Application
{
static App()
{
// First, we set up the internal proxy
SetupInternalProxy();
// The we set up the awesomium engine
SetupBrowser();
}
private static void SetupInternalProxy()
{
// My requirement is to get response content, so I use this event.
// You may use other handlers if you have to tamper data.
FiddlerApplication.AfterSessionComplete += FiddlerApplication_AfterSessionComplete;
FiddlerApplication.Log.OnLogString += (o, s) => Debug.WriteLine(s);
FiddlerCoreStartupFlags oFCSF = FiddlerCoreStartupFlags.Default;
//this line is important as it will avoid changing the proxy for the whole system.
oFCSF = (oFCSF & ~FiddlerCoreStartupFlags.RegisterAsSystemProxy);
FiddlerApplication.Startup(0, oFCSF);
}
private static void SetupBrowser()
{
// We may be a new window in the same process.
if (!WebCore.IsRunning)
{
// Setup WebCore with plugins enabled.
WebCoreConfig config = new WebCoreConfig
{
// Here we plug the internal proxy to the awesomium engine
ProxyServer = "http://127.0.0.1:" + FiddlerApplication.oProxy.ListenPort.ToString(),
// Adapt others options related to your needs
EnablePlugins = true,
SaveCacheAndCookies = true,
UserDataPath = Environment.ExpandEnvironmentVariables(@"%APPDATA%\MyApp"),
};
WebCore.Initialize(config);
}
else
{
throw new InvalidOperationException("WebCore should be already running");
}
}
// Here is the handler where I intercept the response
private static void FiddlerApplication_AfterSessionComplete(Session oSession)
{
// Send to business objects
DoSomethingWith(
oSession.PathAndQuery,
oSession.ResponseBody,
oSession["Response", "Content-Type"]
);
}
}
As I said in the comment, you may use another event handler that AfterSessionComplete. It will depends on your requirement (read the fiddler core SDK to get help).
A final word: this code run from the app class (equivalent of Program class in Winform). You may require to use a messaging system or publish a global event (beware of memory leak) in order to use the result in a windows class. You also have to aware that the AfterSessionComplete event is fired from multiple threads, sometimes simultaneously. You will use some kind of Invoking to work in the UI thread.
Solution 5
HtmlElementCollection elc = WebBrowser1.Document.GetElementsByTagName("img");
foreach (HtmlElement el in elc)
{
if (el.GetAttribute("src") != null)
{
el.SetAttribute("src", "");
}
}
if there is any element that may contain images then it will be in an img
tag.
Mihai Viteazu
Updated on June 30, 2022Comments
-
Mihai Viteazu almost 2 years
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 viawebBrowser1.DocumentText
and usingRegex.Replace
to remove the images, but then it shows me a blank page withaaa
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";