• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

C# 根据路径,保存到指定文件

开发技术 开发技术 2周前 (04-08) 8次浏览
 public void ImgSave()
        {
            string url = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F3d3bbb5626df7c0a52b0e665eea40a45dde19b051b795-9m8Sul_fw658&refer=http%3A%2F%2Fhbimg.b0.upaiyun.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1620482119&t=3e28067466a4e0a77c6e820d01061c77";
            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(res.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();
            }

        }

需引入System.Drawing.Common包


程序员灯塔
转载请注明原文链接:C# 根据路径,保存到指定文件
喜欢 (0)