zoukankan      html  css  js  c++  java
  • 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[^<>]*?src[s	
    ]*=[s	
    ]*[""']?[s	
    ]*(?<imgUrl>[^s	
    ""'<>]*)[^<>]*?/?[s	
    ]*>", 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

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

  • 相关阅读:
    win server 2012 服务器不能ping通
    Linux系统的文件目录结构
    怎样理解和识别 Linux 中的文件类型
    Linux(Centos 7)下安装Git并配置连接GitHub
    centos 7 下升级自带 sqlite3
    Pycharm项目上传到Github
    计算机存储单位:bit, Byte, KB, MB, GB, TB, PB, EB, ZB, BB
    ubuntu 删除命令
    influxDB 各个历史版本号,时间截止2020年9月27日
    使用Github做一个完全免费的个人网站
  • 原文地址:https://www.cnblogs.com/BeiJing-Net-DaiDai/p/10177764.html
Copyright © 2011-2022 走看看