很多微信公众号中需要生成推广海报的功能,粉丝获得专属海报后可以分享到朋友圈或发给朋友,为公众号代言邀请好友即可获取奖励的。海报自带渠道二维码,粉丝长按二维码即可关注微信公众号,从而达到吸粉的目的。

微信图片_20181206210040.jpg

  1. /// <summary>
  2. /// 获取临时二维码ticket
  3. /// </summary>
  4. /// <param name="scene_str">场景值ID openid做场景值ID</param>
  5. /// <returns></returns>
  6. public static string CreateTempQRCode(string scene_str,string access_token)
  7. {
  8. var result = HttpUtility.SendPostHttpRequest($"https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={access_token}", "application/json", "{\"expire_seconds\": 2592000, \"action_name\": \"QR_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": \"" + scene_str + "\"}}}");
  9. JObject jobect = (JObject)JsonConvert.DeserializeObject(result);
  10. string ticket = (string)jobect["ticket"];
  11. if (string.IsNullOrEmpty(ticket))
  12. {
  13. LogHelper.WriteLog(typeof(WeixinHelper), "获取临时二维码ticket失败" + result);
  14. return null;
  15. }
  16. return ticket;
  17. }

使用openid作为场景值的好处是通过扫A推广的二维码关注用户的场景值便是A的openid。

  1. /// <summary>
  2. /// 生成带二维码的专属推广图片
  3. /// </summary>
  4. /// <param name="user"></param>
  5. /// <returns></returns>
  6. public string Draw(WxUser user)
  7. {
  8. //背景图片
  9. string path = Server.MapPath("/Content/images/tg.jpg");
  10. System.Drawing.Image imgSrc = System.Drawing.Image.FromFile(path);
  11. //处理二维码图片大小 240*240px
  12. System.Drawing.Image qrCodeImage = ReduceImage("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket="+user.ticket, 240, 240);
  13. //处理头像图片大小 100*100px
  14. Image titleImage = ReduceImage(user.headimgurl, 100, 100);
  15. using (Graphics g = Graphics.FromImage(imgSrc))
  16. {
  17. //画专属推广二维码
  18. g.DrawImage(qrCodeImage, new Rectangle(imgSrc.Width - qrCodeImage.Width - 200,
  19. imgSrc.Height - qrCodeImage.Height - 200,
  20. qrCodeImage.Width,
  21. qrCodeImage.Height),
  22. 0, 0, qrCodeImage.Width, qrCodeImage.Height, GraphicsUnit.Pixel);
  23. //画头像
  24. g.DrawImage(titleImage, 8, 8, titleImage.Width, titleImage.Height);
  25. Font font = new Font("宋体", 30, FontStyle.Bold);
  26. g.DrawString(user.nickname, font, new SolidBrush(Color.Red), 110, 10);
  27. }
  28. string newpath = Server.MapPath(@"/Content/images/newtg_" + Guid.NewGuid().ToString() + ".jpg");
  29. imgSrc.Save(newpath, System.Drawing.Imaging.ImageFormat.Jpeg);
  30. return newpath;
  31. }
  32. /// <summary>
  33. /// 缩小/放大图片
  34. /// </summary>
  35. /// <param name="url">图片网络地址</param>
  36. /// <param name="toWidth">缩小/放大宽度</param>
  37. /// <param name="toHeight">缩小/放大高度</param>
  38. /// <returns></returns>
  39. public Image ReduceImage(string url, int toWidth, int toHeight)
  40. {
  41. WebRequest request = WebRequest.Create(url);
  42. WebResponse response = request.GetResponse();
  43. Stream responseStream = response.GetResponseStream();
  44. Image originalImage = Image.FromStream(responseStream);
  45. if (toWidth <= 0 && toHeight <= 0)
  46. {
  47. return originalImage;
  48. }
  49. else if (toWidth > 0 && toHeight > 0)
  50. {
  51. if (originalImage.Width < toWidth && originalImage.Height < toHeight)
  52. return originalImage;
  53. }
  54. else if (toWidth <= 0 && toHeight > 0)
  55. {
  56. if (originalImage.Height < toHeight)
  57. return originalImage;
  58. toWidth = originalImage.Width * toHeight / originalImage.Height;
  59. }
  60. else if (toHeight <= 0 && toWidth > 0)
  61. {
  62. if (originalImage.Width < toWidth)
  63. return originalImage;
  64. toHeight = originalImage.Height * toWidth / originalImage.Width;
  65. }
  66. Image toBitmap = new Bitmap(toWidth, toHeight);
  67. using (Graphics g = Graphics.FromImage(toBitmap))
  68. {
  69. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  70. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  71. g.Clear(Color.Transparent);
  72. g.DrawImage(originalImage,
  73. new Rectangle(0, 0, toWidth, toHeight),
  74. new Rectangle(0, 0, originalImage.Width, originalImage.Height),
  75. GraphicsUnit.Pixel);
  76. originalImage.Dispose();
  77. return toBitmap;
  78. }
  79. }
  1. string imagePath = Draw(user);
  2. string result = HttpUtility.UploadFile($"https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={access_token}&type=image", imagePath);
  3. JObject jObject = (JObject)JsonConvert.DeserializeObject(result);
  4. string media_id = (string)jObject["media_id"];
  5. if (!string.IsNullOrEmpty(media_id))
  6. {
  7. string resxml = "<xml><ToUserName><![CDATA[" + xmlMsg.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + xmlMsg.ToUserName + "]]></FromUserName><CreateTime>" + nowtime + "</CreateTime><MsgType><![CDATA[image]]></MsgType><Image><MediaId><![CDATA[" + media_id + "]]></MediaId></Image></xml>";
  8. return resxml;

}
LogHelper.WriteLog(typeof(WechatController), “上传专属推广图片素材失败” + result);

详细请查看 http://blog.yshizi.cn/50.html

版权声明:本文为gracexu原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/gracexu/p/10081111.html