首页 > Windows开发 > 详细

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

时间:2021-04-09 00:06:50      阅读:105      评论:0      收藏:0      [点我收藏+]
 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# 根据路径,保存到指定文件

原文:https://www.cnblogs.com/yueyongsheng/p/14635180.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!