[英]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.