问题描述
是否可以将网络浏览器控件中的图像直接保存到硬盘,而无需再次从 Internet 下载?
Is it possible to save images in a webbroswer control directly to hard disk, without needing to download them again from the internet?
假设我导航到一个有 15 张图片的网站.它们都在我的网络浏览器中查看,但我现在如何保存它们而不需要下载它们?
Let's say I navigate to a website that has 15 images. They are all viewed in my webbrowser, but how can I save them now without the need to download them?
推荐答案
这是我能找到的唯一方法.想知道其他人是否有更好的方法.
This is the only way I could find. Curious if anyone else has a better way.
复制自 CodeProject
您必须添加对 Microsoft.mshtml 的引用,当然还要等待文档完成加载.这将保存在 System.Windows.Forms.WebBrowser webBrowser1
组件中加载的图像 - 甚至是您不想要的图像.
You have to add a reference to Microsoft.mshtml and of course wait for the document to finish loading. This will save the images loaded in a System.Windows.Forms.WebBrowser webBrowser1
component - even the ones you don't want.
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);
}
}
这篇关于将图像保存在 webbrowser 控件中,而无需从 Internet 重新下载它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!