首页 > 代码库 > C# 下载远程图片并显示

C# 下载远程图片并显示

/// <summary>
        /// 下载远程图片
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        private Image GetImage(string url)
        {
            try
            {
                HttpWebRequest myrequest = WebRequest.Create(url) as HttpWebRequest;
                HttpWebResponse resp = (HttpWebResponse)myrequest.GetResponse();
                if (resp.StatusCode == HttpStatusCode.OK)
                {
                    Stream imgstream = resp.GetResponseStream();
                    return System.Drawing.Image.FromStream(imgstream);
                }
                else
                {
                    return null;
                }
            }  
            catch (WebException ex)
            {
                return null;
            }
            catch (Exception ex)
            {
                return null;
            }

        }

 

C# 下载远程图片并显示