How to block images in a web browser

I am building a C # c application WebBrowserand I am trying to figure out a way to block images, i.e. so that they do not appear when loading the website (so that the website loads easier).

I tried to remove the tags <img>, getting it through webBrowser1.DocumentTextand using Regex.Replaceto delete the images, but then it shows me a blank page with aaawhen I use the code, Is there a better way to delete the images? Any help was greatly appreciated.

The code:

var html_source = webBrowser1.DocumentText;
var newOne = Regex.Replace(html_source, "<img.*/>", "", RegexOptions.Multiline);
webBrowser1.DocumentText = newOne + "aaa";

Update:

I tried under the code (for testing only), but still shows me only aaa.

var html_source = webBrowser1.DocumentText;
webBrowser1.DocumentText = html_source + "aaa";
+5
source share
6 answers

EDIT

SO , codeproject.com. userControl COM- webBrowser. , , .net Framework WebBrowser. , HTML-.

... - IDispatch_Invoke_Handler().... IDispatch:: Invoke, , IE (, , ActiveX, Java). , IDispatch_Invoke_Handler() COM -5512, . , .

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
     Debug.WriteLine("documentCompleted");
     HtmlDocument doc = webBrowser1.Document;
     foreach (HtmlElement  imgElemt in doc.Images)
     {
         imgElemt.SetAttribute("src", "");
     }
}

, MSDN

DocumentCompleted, , . DocumentCompleted, , , Document, DocumentText DocumentStream .

, webBrowser .NET Framework.

+4

:

private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
{
  if (webBrowser1.Document != null)
  {
     foreach (HtmlElement imgElemt in webBrowser1.Document.Images)
     {
        imgElemt.SetAttribute("src", "");
     }
   }
}
+4

- , Internet Explorer.

, , -, - ( , Internet explorer).

:

1.) open integent explorer

2.) 'tools' > 'internet options'

3.) "advanced"

4.) , " " ( "" )

, , . , .

+3

webbrowser. , , , .

:

Awesomium, , . .

Fiddler . API /tamper/... . -, mime- HTML DOM ( HtmlAgility pack!!!!!) 200 http .

, . - WPF, winform :

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"]
            );

    }
}

, AfterSessionComplete. ( SDK Fiddler, ).

: ( Program Winform). ( ), Windows. , AfterSessionComplete , . - Invoking .

+1
HtmlElementCollection elc = WebBrowser1.Document.GetElementsByTagName("img");
foreach (HtmlElement el in elc)
{
   if (el.GetAttribute("src") != null)
   {
       el.SetAttribute("src", "");
   }
}

- , , img.

+1
source

For this purpose you can use the Lazy Load loading technique. See http://engineering.slideshare.net/2011/03/faster-page-loads-with-image-lazy-loading/

0
source

All Articles