/// <summary>
/// 根据路径下载图片
/// </summary>
/// <param name="url">路径</param>
public void ImgSave(string url)
{
try
{
int count = 10000;
while (true)
{
// url = "https://thispersondoesnotexist.com/image";
WebRequest imgRequest = WebRequest.Create(url);
HttpWebResponse res;
try
{
res = (HttpWebResponse)imgRequest.GetResponse();
}
catch (WebException ex)
{
res = (HttpWebResponse)ex.Response;
}
if (res.StatusCode.ToString() == "OK")
{
System.Drawing.Image downImage = System.Drawing.Image.FromStream(imgRequest.GetResponse().GetResponseStream());
string deerory = string.Format(@"D:\img\{0}\", DateTime.Now.ToString("yyyy-MM-dd"));
string fileName = string.Format("{0}.png", DateTime.Now.ToString("HHmmssffff"));
if (!System.IO.Directory.Exists(deerory))
{
System.IO.Directory.CreateDirectory(deerory);
}
downImage.Save(deerory + fileName);
downImage.Dispose();
}
if (count > 10000)
{
break;
}
}
}
catch (Exception ex)
{
}
}