[英]How to get image size on random page using C# with MSHTML?
我正在尝试使用mshtml检索页面上的图像。 在2台不同的计算机(均为Win7-64位)上工作会给我带来不同的结果。 其中一台机器工作正常。 但是,第二个无法读取图像的width / height属性。 所有的高度/宽度均为零。
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);
}
}
运行正常的计算机使用MSDN Visual Studio2010。未提供正确结果的计算机使用Visual Studio Express Edition 2010。
我将非常感谢您的帮助:如何在两台计算机上获取图像的大小?
这可能是由于该计算机上安装的MSHTML引擎版本不同所致。 验证版本相同。 另外,在尝试从元素中读取属性之前,请确保文档已完全加载。
也就是说,为什么只使用重量级的MSHTML控件读取图像尺寸? 为什么不使用免费,轻量级的开源HTML Agility Pack ?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.