简体   繁体   English

如何在MSHTML上使用C#获取随机页面上的图像大小?

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

I am trying to retrieve images on the page using mshtml. 我正在尝试使用mshtml检索页面上的图像。 Working on 2 different machines (both Win7-64 bit) gives me different results. 在2台不同的计算机(均为Win7-64位)上工作会给我带来不同的结果。 One of the machines works just fine. 其中一台机器工作正常。 The second one, though, is unable to read the width/height attribute of images. 但是,第二个无法读取图像的width / height属性。 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. 运行正常的计算机使用MSDN Visual Studio2010。未提供正确结果的计算机使用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. 这可能是由于该计算机上安装的MSHTML引擎版本不同所致。 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? 也就是说,为什么只使用重量级的MSHTML控件读取图像尺寸? Why not use the free, lightweight, open source HTML Agility Pack ? 为什么不使用免费,轻量级的开源HTML Agility Pack

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C#中使用MSHTML获取当前样式 - Get current style using MSHTML in C# 如何使用mshtml从c#windows app以编程方式在网页中写入文件类型的输入字段? - How to write to a input field of type file in a web page programatically from a c# windows app using mshtml? 在C#中使用mshtml从HTML获取href - Get href from html using mshtml in C# 在C#中使用不同版本的MSHTML - Using different versions of MSHTML in C# 如何打开Internet Explorer窗口,在URL上导航并使用c#和mshtml库获取她的文档(HTMLDocument或InternetExplorer) - How open internet explorer window, navigate it on url and get her document(HTMLDocument or InternetExplorer) using c# and mshtml library 在Windows窗体中使用C#在编辑模式下使用MSHTML WebBrowser时,如何检测文档更改? - How to detect document changes when using the MSHTML WebBrowser in edit mode using C# in Windows Forms? 如何通过C#从mshtml元素获取所有已应用的CSS样式信息 - How to get all applied CSS Style info from mshtml element via C# 如何在C#中使用mshtml设置文档的第一形式的第一输入类型文本的值? - How to set the value of first input type text in document's first form using mshtml in C#? 如何使用 mshtml(c# 代码)关闭新打开的 Internet Explorer - How can I close the newly opened Internet Explorer using the mshtml (c# code) 在HTML文件C#MSHTML中获取嵌入参数 - Get Embed Parameters in a HTML File C# MSHTML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM