• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
半壁江山
博客园    首页    新随笔    联系   管理    订阅  订阅

抓取网络图片保存到本地

#region C#抓取网络图片保存到本地
public static Bitmap GetRequestImg()
{
Bitmap img = null;
HttpWebRequest req;
HttpWebResponse res = null;
try
{
System.Uri httpUrl = new System.Uri("https://s3-us-west-1.amazonaws.com//web.noblehouse/53587.00/53587.00_1_th.jpg");
req = (HttpWebRequest)(WebRequest.Create(httpUrl));
req.Timeout = 180000; //设置超时值10秒
req.UserAgent = "XXXXX";
req.Accept = "XXXXXX";
req.Method = "GET";
res = (HttpWebResponse)(req.GetResponse());
img = new Bitmap(res.GetResponseStream());//获取图片流
string filePath = @"E:\ad";
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
//img.Save(@"E:/" + DateTime.Now.ToFileTime().ToString() + ".jpg");//随机名
img.Save(filePath + @"\"+ DateTime.Now.ToFileTime().ToString() + ".jpg");//随机名
}

catch (Exception ex)
{
string aa = ex.Message;
}
finally
{
res.Close();
}
return img;
}


#endregion

 

posted @ 2017-05-25 16:06  半壁江山  阅读(546)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3