这是我正在使用的功能,我想将图像保存在我从网站上获得的链接中的硬盘上。
public void GetAllImages()
{
// Bing Image Result for Cat, First Page
string url = "http://www.bing.com/images/search?q=cat&go=&form=QB&qs=n";
// For speed of dev, I use a WebClient
WebClient client = new WebClient();
string html = client.DownloadString(url);
// Load the Html into the agility pack
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
// Now, using LINQ to get all Images
/*List<HtmlNode> imageNodes = null;
imageNodes = (from HtmlNode node in doc.DocumentNode.SelectNodes("//img")
where node.Name == "img"
&& node.Attributes["class"] != null
&& node.Attributes["class"].Value.StartsWith("sg_t")
select node).ToList();*/
var imageLinks = doc.DocumentNode.Descendants("img")
.Where(n => n.Attributes["class"].Value == "sg_t")
.Select(n => HttpUtility.ParseQueryString(n.Attributes["src"].Value["amp;url"])
.ToList();
foreach (string node in imageLinks)
{
y++;
//Console.WriteLine(node.Attributes["src"].Value);
richTextBox1.Text += node + Environment.NewLine;
Bitmap bmp = new Bitmap(node);
bmp.Save(@"d:\test\" + y.ToString("D6") + ".jpg");
}
}
在 foreach 的底部,我使用 Bitmap,但后来我得到了错误。为什么 ?