从网络路径下载图片

using System;
using System.Drawing;
using System.IO;
using System.Net;

 

///


    /// 图片操作
    ///

    public class ImageDownload
    {

        ///
        /// 从网络下载图片并保存到指定路径
        ///

        /// 图片网络Url
        /// 本地路径
        /// 图片名称
        public void DownloadImageAndSavaToLoca(string param_WebImageUrl, string param_Localpath, string param_SavaImageFileNmae)
        {
            WebRequest ImageResqust = WebRequest.Create(param_WebImageUrl);
            HttpWebResponse res;
            try
            {
                res = (HttpWebResponse)ImageResqust.GetResponse();
            }
            catch (WebException ex)
            {

                res = (HttpWebResponse)ex.Response;
            }
            try
            {
                //图片下载成功
                if (res.StatusCode.ToString() == "OK")
                {//以流的方式保存图片
                    Image downimage = Image.FromStream(ImageResqust.GetResponse().GetResponseStream());
                    //------保存图片到本地------
                    if (!Directory.Exists(param_Localpath))
                    {
                        Directory.CreateDirectory(param_Localpath);
                    }
                    downimage.Save(param_Localpath + param_SavaImageFileNmae);
                    downimage.Dispose();
                }
            }
            catch (Exception ex)
            {


            }

        }

    }

你可能感兴趣的:(从网络路径下载图片)