微信的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
更多方式了解请加页面下方的群