码迷,mamicode.com
首页 > 微信 > 详细

C#获取微信二维码显示到wpf

时间:2019-01-02 13:34:35      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:ons   reg   count   ring   read   web   dai   value   今天   

原文:C#获取微信二维码显示到wpf

微信的api开放的二维码是一个链接地址,而我们要将这个二维码显示到客户端。方式很多,今天我们讲其中一种。

 /// <summary>
        /// 获取图片路径
        /// </summary>
        /// <param name="httpUrl"></param>
        /// <returns></returns>
        public string GetImageUrl(string httpUrl)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(httpUrl);
            req.ServicePoint.Expect100Continue = false;
            req.Method = "GET";
            req.KeepAlive = true;
            req.ContentType = "image/png";
            HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(rsp.GetResponseStream(), Encoding.GetEncoding("utf-8"));
            string str = reader.ReadToEnd();
            string[] imgStr = GetHtmlImageUrlList(str);
            string strer = "https://open.weixin.qq.com" + imgStr[0];
            return strer;
        }
        /// <summary>
        /// 获取img标签
        /// </summary>
        /// <param name="sHtmlText"></param>
        /// <returns></returns>
        public string[] GetHtmlImageUrlList(string sHtmlText)
        {
            Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""‘]?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""‘<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
            MatchCollection matches = regImg.Matches(sHtmlText);
            int i = 0;
            string[] sUrlList = new string[matches.Count];
            foreach (Match match in matches)
                sUrlList[i++] = match.Groups["imgUrl"].Value;
            return sUrlList;
        }

调用方式:

string imgUrl= GetImageUrl("https://open.weixin.qq.com/......微信地址");
            img.Source = new BitmapImage(new Uri(imgUrl));

这个是访问微信地址url,获取到这个url中显示的微信二维码,拿到这个图片,显示到wpf

更多方式了解请加页面下方的群

C#获取微信二维码显示到wpf

标签:ons   reg   count   ring   read   web   dai   value   今天   

原文地址:https://www.cnblogs.com/lonelyxmas/p/10208006.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!