微信的api开放的二维码是一个链接地址,而我们要将这个二维码显示到客户端。方式很多,今天我们讲其中一种。
1/// <summary> 2 /// 获取图片路径 3 /// </summary> 4 /// <param name="httpUrl"></param> 5 /// <returns></returns> 6 public string GetImageUrl(string httpUrl) 7 { 8 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(httpUrl); 9 req.ServicePoint.Expect100Continue = false; 10 req.Method = "GET"; 11 req.KeepAlive = true; 12 req.ContentType = "image/png"; 13 HttpWebResponse rsp = (HttpWebResponse)req.GetResponse(); 14 StreamReader reader = new StreamReader(rsp.GetResponseStream(), Encoding.GetEncoding("utf-8")); 15 string str = reader.ReadToEnd(); 16 string[] imgStr = GetHtmlImageUrlList(str); 17 string strer = "https://open.weixin.qq.com" + imgStr[0]; 18 return strer; 19 } 20 /// <summary> 21 /// 获取img标签 22 /// </summary> 23 /// <param name="sHtmlText"></param> 24 /// <returns></returns> 25 public string[] GetHtmlImageUrlList(string sHtmlText) 26 { 27 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); 28 MatchCollection matches = regImg.Matches(sHtmlText); 29 int i = 0; 30 string[] sUrlList = new string[matches.Count]; 31 foreach (Match match in matches) 32 sUrlList[i++] = match.Groups["imgUrl"].Value; 33 return sUrlList; 34 }
调用方式:
1string imgUrl= GetImageUrl("https://open.weixin.qq.com/......微信地址"); 2 img.Source = new BitmapImage(new Uri(imgUrl));
这个是访问微信地址url,获取到这个url中显示的微信二维码,拿到这个图片,显示到wpf
更多方式了解请加页面下方的群