当前位置:   article > 正文

C#生成微信小程序文章二维码_c# 生成小程序二维码

c# 生成小程序二维码

  1. /// <summary>
  2. /// 获取access_token
  3. /// </summary>
  4. /// <returns></returns>
  5. [HttpGet]
  6. [Route("GetAccessToken")]
  7. public async Task<ActionResult> GetAccessToken()
  8. {
  9. string token = string.Empty;
  10. try
  11. {
  12. string wechatapi = "https://api.weixin.qq.com/";
  13. string appID = "wxc1aac131465e16b5";
  14. string appSecret = "ef392ad696f8ccbfd6659758ebead29c";
  15. //获取微信token
  16. string token_url = wechatapi + "cgi-bin/token?grant_type=client_credential&appid=" + appID + "&secret=" + appSecret;
  17. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(token_url);
  18. request.Method = "GET";
  19. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  20. Stream getImgStream = response.GetResponseStream();
  21. StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
  22. string content = reader.ReadToEnd();
  23. response.Close();
  24. reader.Dispose();
  25. token = content;
  26. }
  27. catch (Exception ex)
  28. {
  29. }
  30. return Ok(token);
  31. }
  32. /// <summary>
  33. /// B接口-微信小程序带参数二维码的生成
  34. /// </summary>
  35. /// <param name="access_token"></param>
  36. /// <returns></returns>
  37. [HttpGet]
  38. [Route("CreateWxCode")]
  39. public async Task<ActionResult> CreateWxCode(string access_token)
  40. {
  41. //适用于需要的码数量极多,或仅临时使用的业务场景
  42. //通过该接口生成的小程序码,永久有效,数量暂无限制。
  43. string postUrl = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + access_token;
  44. HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;
  45. request.Method = "POST";
  46. request.ContentType = "application/json;charset=UTF-8";
  47. string options = "{\"scene\":\"" + 1 + "\",\"page\":\"pages/home/home\",\"width\":\"150\"}";
  48. byte[] payload = Encoding.UTF8.GetBytes(options);
  49. request.ContentLength = payload.Length;
  50. Stream writer = request.GetRequestStream();
  51. writer.Write(payload, 0, payload.Length);
  52. writer.Close();
  53. System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
  54. System.IO.Stream stream = response.GetResponseStream();
  55. List<byte> bytes = new List<byte>();
  56. int temp = stream.ReadByte();
  57. while (temp != -1)
  58. {
  59. bytes.Add((byte)temp);
  60. temp = stream.ReadByte();
  61. }
  62. byte[] result = bytes.ToArray();
  63. string base64 = Convert.ToBase64String(result);//将byte[]转为base64
  64. var QRcode = ToImage(base64);
  65. CombineBitmap("D://EWM/immm/222.jpg", QRcode);
  66. return Ok("{\"errcode\":0,\"errmsg\":\"获取成功\",\"buffer\":\"" + base64 + "\"}");
  67. }
  68. private string ToImage(string base64String)
  69. {
  70. byte[] bytes = Convert.FromBase64String(base64String);
  71. using (Image image = Image.FromStream(new MemoryStream(bytes)))
  72. {
  73. var imgName = DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";
  74. image.Save("D://EWM/" + imgName, System.Drawing.Imaging.ImageFormat.Jpeg); // Or Png
  75. return "D://EWM/" + imgName;
  76. }
  77. }
  78. public string CombineBitmap(string backgroundImg,string FrontImg)
  79. {
  80. var imgName = DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";
  81. String jpg3 = "D://EWM/" + imgName;
  82. Image img1 = Image.FromFile(FrontImg);
  83. Image img2 = Image.FromFile(backgroundImg);
  84. int width = img2.Width;
  85. int height = Math.Max(img1.Height, img2.Height);
  86. Bitmap img3 = new Bitmap(width, height);
  87. Graphics g = Graphics.FromImage(img3);
  88. g.Clear(Color.Black);
  89. g.DrawImage(img2, new Point(0, 0));
  90. g.DrawImage(img1, new Point(img2.Width - img1.Width, img2.Height - img1.Height));
  91. System.Drawing.Text.PrivateFontCollection font = new System.Drawing.Text.PrivateFontCollection();
  92. font.AddFontFile("E:/Orchard_GOV/OnonMRMS/src/OrchardCore.Onon/Onon.Gov/wwwroot/Fonts/MQG.ttf");
  93. //检测字体类型是否可用
  94. var r = font.Families[0].IsStyleAvailable(FontStyle.Regular);
  95. var b = font.Families[0].IsStyleAvailable(FontStyle.Bold);
  96. //定义成新的字体对象
  97. FontFamily myFontFamily = new FontFamily(font.Families[0].Name, font);
  98. Font myFont = new Font(myFontFamily, 22, FontStyle.Bold);
  99. //将字体显示到控件
  100. //g.DrawString("这里输入文字", font, new SolidBrush(Color.Red), 15, 15);
  101. g.TranslateTransform(15, 15);
  102. //设置旋转角度
  103. g.RotateTransform(90);
  104. //画文字
  105. g.DrawString("ᠮᠣᠩᠭᠣᠯ ᠬᠥᠮᠥᠨ ᠡᠨᠳᠡ ᠪᠠᠢᠨ᠎ᠠ", myFont, new SolidBrush(Color.White),60, -60);
  106. g.ResetTransform();
  107. g.Dispose();
  108. img2.Dispose();
  109. img1.Dispose();
  110. img3.Save(jpg3, System.Drawing.Imaging.ImageFormat.Jpeg);
  111. img3.Dispose();
  112. return jpg3;
  113. }
  114. -----GT

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/507772
推荐阅读
相关标签
  

闽ICP备14008679号