如何使用 C# 和 MSHTML 获取随机页面上的图像大小?
我正在尝试使用 mshtml 检索页面上的图像。在两台不同的机器(都是 Win7-64 位)上工作给了我不同的结果。其中一台机器运转良好。但第二个无法读取图像的宽度/高度属性。所有高度/宽度均为零。
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 Studio 2010。没有给我正确结果的机器使用 Visual Studio Express Edition 2010。
我将非常感谢任何帮助:如何获取两台机器上图像的大小?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是由于该计算机上安装的 MSHTML 引擎版本不同所致。验证版本是否相同。另外,在尝试从元素读取属性之前,请验证文档是否已完全加载。
也就是说,为什么要使用重量级 MSHTML 控件来读取图像尺寸?为什么不使用免费、轻量级、开源的 HTML Agility Pack?
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?