.NET下载网页图片

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/zhulongxi/article/details/52038276
两种方式:WebRequest 和 WebClient:
 
  public void DownloadImage(string url, string path)
        {
            WebRequest request = WebRequest.Create(url);
            request.ContentType = "impage/jpg";
            WebResponse response = request.GetResponse();
            using (Stream reader = response.GetResponseStream())
            {
                using (FileStream writer = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    byte[] buffer = new byte[512];
                    int c = 0;
                    while ((c = reader.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        writer.Write(buffer, 0, c);
                    }
                }
            }


        }

        //public void DownloadImage(string url, string path)
        //{
        //    WebClient client = new WebClient();
        //    client.DownloadFile(url, path);
        //}


展开阅读全文

没有更多推荐了,返回首页