Webbrowser control: how to disable multimedia?

J. Scott Elblein

Well-known member
Joined
Dec 14, 2006
Messages
166
Location
Chicago
Programming Experience
10+
Hello all, another question about the webbrowser control :)

In the interest of speeding up page downloads, I would like to not download any images (jpg, gif, etc.) as well as any flash files, etc.

How can I do this?

Also, IE (an consequently the webbrowser control as well) plays a "click" sound when you click a link, and often times on it's own. I would also like to disable and sounds emanating form the control. The main purpose in my project is to simply use the control to silently (it will be hidden) navigate pages and use it's source code for parsing. I need to use the webbrowser control over other methods due to cookie handling, redirects and other riff raff.

Thanks in advance.!
 
Try this, reset the image source link on load, Webbrowser.DocumentCompleted happens when the document string is loaded, but before images are.
VB.NET:
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As WebBrowserDocumentCompletedEventArgs) _
Handles WebBrowser1.DocumentCompleted
    For Each img As HtmlElement In WebBrowser1.Document.Images
        img.SetAttribute("src", "")
    Next
End Sub
There are also other html elements that may contain images, like backgrounds. And you sometimes got iframes loading different documents/ads. You have to look into how flash is tagged into document and modify that also.
 
Thanks again John. An interesting thing is happening when I use this code.

As the page downloads, all of the images are being shown as usual, however once the page is done loading, it appears to go through all of the images and then remove them, leaving the IE red X in their stead.

I do understand the concept: try to set the image source to nothing as it downloads so that the image doesn't get downloaded, but I am wondering if DocCompleted is the right place?

Another weird thing, when I place this code in DocCompleted:
VB.NET:
 If wbr.ReadyState = WebBrowserReadyState.Complete Then MessageBox.Show("Done")

It simply never fires the message box?
 
It does when I try. DocumentCompleted fires first time when ReadyState becomes "complete" for the initial page request, this is also the first moment of a page load where the DOM document is available for you, before this there is no "document" object to interrogate. About seeing some images, its inevitable because loading is so fast, by the time the loop is finished there are some images shown and then 'X'ed.
 
Hmmm, so if I am understanding this correctly, which I may not be, there is no real way to actually stop the actual downloading of the images before the page is downloaded? Even though the download is fast, the fact that the pictures show up at all means they are still getting downloaded with the page and probably causing some hit to speed, whether major or minor.

About the readystate, maybe it's because of the website you tested on? I like to test things like this on "difficult" pages like myspace.com, or something like yahoo.

One last question not related to the original: does the httpwebrequest/response class use any of the cookie/cache data from the webbrowser control? I am wondering if once I log into a site using the webbrowser control, if it would be faster to use the webrequest classes to get pure source code, but I need to stay logged in. I am assuming the answer is no, but just want to verify w/someone who knows for sure.
 
"readystate" is only for the document string (and loading the nodes into the document object), and not related to what the control does afterwards when parsing and downloading linked content like images.

httpwebrequest/response is self contained and does not grab anything from other user sessions.
 
Back
Top