• .net获取网络图片


    using System.IO;
    using System.Net;
    
    namespace TestUpfile
    {
        public class NetHelper
        {
            public static Stream GetImgStream(string url,string protocol="http")
            {
                WebResponse response = null;
                Stream stream = null;
                try
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                    response = request.GetResponse();
                    stream = response.GetResponseStream();
                    if (response.ContentType.ToLower().StartsWith("text/")) return null;
                    ////如果要转成byte
                    //using (MemoryStream mstream = new MemoryStream())
                    //{
                    //    int count = 0;
                    //    byte[] buffer = new byte[1024];
                    //    int readnum = 0;
                    //    while ((readnum = stream.Read(buffer, 0, 1024)) > 0)
                    //    {
                    //        count = count + readnum;
                    //        mstream.Write(buffer, 0, 1024);
                    //    }
                    //    mstream.Position = 0;
                    //    using (BinaryReader br = new BinaryReader(mstream))
                    //    {
                    //        byte[] bytes = br.ReadBytes(count);
                    //    }
                    //}
                    return stream;
                }
                catch
                {
    
                }
                return stream;
            }
    
            /// <summary>
            /// 通过webClient获取图片的字节
            /// </summary>
            /// <param name="url">要下载的url</param>
            /// <param name="protocol"></param>
            /// <returns></returns>
            public static byte[] GetImgBytes(string url)
            {
                byte[] bytes = null;
                try
                {
                    if (url.Substring(0,5).ToLower() == "https")
                    {
                        // 解决WebClient不能通过https下载内容问题
                        System.Net.ServicePointManager.ServerCertificateValidationCallback +=
                            delegate (object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
                                     System.Security.Cryptography.X509Certificates.X509Chain chain,
                                     System.Net.Security.SslPolicyErrors sslPolicyErrors)
                            {
                                return true; // **** Always accept
                            };
                    }
                    WebClient client = new WebClient();
                    bytes=client.DownloadData(url);
                    return bytes;
                }
                catch
                {
                    return null;
                }
            }
        }
    }
    

      

  • 相关阅读:
    sublime代码对齐
    sql查询结果存入DataTable,然后从DataTable取数据
    null和System.DBNull.Value的区别
    引用EChart和Bootstrap
    SSIS系列文章收藏
    Object类型的怎么判断空值
    DataGridView刷新数据
    WinFrom弹出输入框
    DataGridView更新数据到数据库
    SqlParameter防止SQL注入
  • 原文地址:https://www.cnblogs.com/ymworkroom/p/11851446.html
Copyright © 2020-2023  润新知