IHTMLDocument2 does not work everywhere

SMUsamaShah picture SMUsamaShah · Aug 28, 2011 · Viewed 9.7k times · Source

In a winform application I have WebBrowser control and a PictureBox. I am loading an Image (from webbrowser control after DocumentComplete) to pictureBox1 with the code below

IHTMLDocument2 doc = (IHTMLDocument2)webBrowser1.Document.DomDocument;
IHTMLControlRange imgRange = (IHTMLControlRange)((HTMLBody)doc.body).createControlRange();

foreach (IHTMLImgElement img in doc.images)
    {

         imgRange.add((IHTMLControlElement)img);

         imgRange.execCommand("Copy", false, null);

         pictureBox1.Bitmap = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap);

     }

Image is displayed in picturebox1 on my pc, but not on my friend's. On another windows XP it worked well after installing VS2008. So I guess IHTMLDocument2 needs a windows update to work correctly.

Which update is that? Or any other alternative to this code in which i don't have to redownload image from internet?

Answer

Erik Philips picture Erik Philips · Aug 28, 2011

Sounds like you need to deploy additional files (dlls?) available Here.

The best option (and what most people do) is to use something like System.Net.WebClient to download the source, and the use WebClient again to download the image.

If you are determined to use an interface for HTML IHTMLDocument2 the file that contains this interface is in C:\Program Files (x86)\Microsoft.NET\Primary Interop Assemblies\Microsoft.mshtml.dll (without the x86 if you are running 32-bit Windows). Include this file in your project if you are using a VS Deployment project (or copy it to the running application directory manually).