是否可以将webbroswer控件中的图像直接保存到硬盘,而无需再次从互联网上下载?
假设我导航到一个有15张图片的网站。它们都在我的webbrowser中查看,但是如何在不需要下载的情况下保存它们呢?
答案 0 :(得分:13)
这是我能找到的唯一方法。好奇,如果其他人有更好的方法。
复制您必须添加对 Microsoft.mshtml 的引用,当然还要等待文档完成加载。这将保存System.Windows.Forms.WebBrowser webBrowser1
组件中加载的图像 - 甚至是您不想要的图像。
IHTMLDocument2 doc = (IHTMLDocument2) webBrowser1.Document.DomDocument;
IHTMLControlRange imgRange = (IHTMLControlRange) ((HTMLBody) doc.body).createControlRange();
foreach (IHTMLImgElement img in doc.images)
{
imgRange.add((IHTMLControlElement) img);
imgRange.execCommand("Copy", false, null);
using (Bitmap bmp = (Bitmap) Clipboard.GetDataObject().GetData(DataFormats.Bitmap))
{
bmp.Save(@"C:\"+img.nameProp);
}
}