从 Web 浏览器加载图像,无需重新下载或复制
我目前正在从剪贴板复制以从浏览器加载图像,
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);
}
}
但使用剪贴板有一些问题。还有其他方法可以做到这一点吗?在 Internet Explorer 中,所有图像都会转到临时目录,如果这里发生同样的情况,有什么方法可以获取已保存图像的路径吗?
I am currently copying from clip board to load image from browser
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);
}
}
But it has some problems using clipboard. Is there any other way to do that. In internet explorer all images go to temp directory, if same happens here is there any way to get path of that saved image?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于页面上的每个图像,您可以收集其 URL,然后调用 WinINET 缓存枚举函数(例如 FindFirstURLCacheEntry)以在 Temporary Internet Files 文件夹中找到后备缓存文件。但是,无法保证该文件将位于缓存中,因为它可能已通过禁止缓存等的标头进行传递。
您可以考虑的一种方法是在应用程序中使用 FiddlerCore (www.fiddler2.com/core);然后,您可以“离线”获取所有传输的图像,并对它们执行任何您想要的操作。
For each image on the page, you can collect its URL then call the WinINET cache enumeration functions (e.g. FindFirstURLCacheEntry) to locate the backing cache file in the Temporary Internet Files folder. However, there's no guarantee that the file will be in the cache because it may have been delivered with headers that forbid caching, etc.
One approach you could consider is using FiddlerCore (www.fiddler2.com/core) in your application; you could then snag all transferred images "off the wire" and do whatever you want with them.