简体   繁体   中英

How to get image size on random page using C# with MSHTML?

I am trying to retrieve images on the page using mshtml. Working on 2 different machines (both Win7-64 bit) gives me different results. One of the machines works just fine. The second one, though, is unable to read the width/height attribute of images. All the heights/width are zeros.

public JsonResult GetHtml(string url)
{
    var client = new WebClient();
    var htmlCode = client.DownloadString(url);
    var htmlDocument = new mshtml.HTMLDocument() as mshtml.IHTMLDocument2;
    htmlDocument.write(htmlCode);
    var htmlImages = htmlDocument.body.all.tags("img");
    var listImages = new List<HtmlImage>();
    foreach (var htmlImage in htmlImages)
    {
        Console.Out.WriteLine("Src: {0}", htmlImage.src);
        Console.Out.WriteLine("Width: {0}", htmlImage.width);
        Console.Out.WriteLine("Height: {0}", htmlImage.height);
    }
}

The machine that works fine uses MSDN Visual Studio 2010. The machine that does not give me the correct results uses Visual Studio Express Edition 2010.

I will really appreciate any help: how can I get the size of the image on both machines?

It's probably due to different versions of the MSHTML engine installed on that machine. Verify the versions are the same. Also, verify the document is fully loaded before trying to read attributes from elements.

That said, why are you using the heavyweight MSHTML control just to read the image dimensions? Why not use the free, lightweight, open source HTML Agility Pack ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM