下载指定url文件或转化为数据流与byte数组

                   #region 生成图片
                    Stream stream = null;
                    string UrlImg = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + QRCode_WxHelper.CreateQRCode(QRmodel.QudaoId);
                    WebClient webClient = new WebClient();
                    webClient.Credentials = CredentialCache.DefaultCredentials;
                    //以数组的形式下载指定文件
                    byte[] byteData = webClient.DownloadData(UrlImg); 
                    stream = BytesToStream(byteData);
                    string str64= System.Convert.ToBase64String(byteData);
                    #endregion 
        /// <summary>
        /// 将二进制转化为数据流
        /// </summary>
        /// <param name="bytes">二进制数组</param>
        /// <returns></returns>
        public Stream BytesToStream(byte[] bytes)
        {
            Stream stream = new MemoryStream(bytes);
            return stream;
        }
        /// <summary>
        /// 将流转化为二进制数组
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public byte[] StreamToBytes(Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            // 设置当前流的位置为流的开始   
            stream.Seek(0, SeekOrigin.Begin);
            return bytes;
        }

你可能感兴趣的:(二进制,数据,Stream,下载,byte)