所以我试图获取C#代码以从网站中选择随机图像,但我不知道如何获取该程序以获取图像URL
我已经尝试过this thread上的代码,但似乎可以使它工作
private List<string> retrieveImages()
{
List<string> imgList = new List<string>();
HtmlDocument doc = new HtmlDocument();
doc.Load("file.htm"); //or whatever HTML file you have
HtmlNodeCollection imgs = doc.DocumentNode.SelectNodes("//img[@src]");
if (imgs == null) return new List<string>();
foreach (HtmlNode img in imgs)
{
if (img.Attributes["src"] == null)
continue;
HtmlAttribute src = img.Attributes["src"];
imgList.Add(src.Value);
//Do something with src.Value such as Get the image and save it locally
// Image img = GetImage(src.Value)
// img.Save(aLocalFilePath);
}
return imgList;
}
private Image GetImage(string url)
{
System.Net.WebRequest request = System.Net.WebRequest.Create(url);
System.Net.WebResponse response = request.GetResponse();
System.IO.Stream responseStream = response.GetResponseStream();
Bitmap bmp = new Bitmap(responseStream);
responseStream.Dispose();
return bmp;
}
程序关闭无错误