赞
踩
- /// <summary>
- /// 获取access_token
- /// </summary>
- /// <returns></returns>
- [HttpGet]
- [Route("GetAccessToken")]
- public async Task<ActionResult> GetAccessToken()
- {
- string token = string.Empty;
- try
- {
- string wechatapi = "https://api.weixin.qq.com/";
- string appID = "wxc1aac131465e16b5";
- string appSecret = "ef392ad696f8ccbfd6659758ebead29c";
- //获取微信token
- string token_url = wechatapi + "cgi-bin/token?grant_type=client_credential&appid=" + appID + "&secret=" + appSecret;
-
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(token_url);
- request.Method = "GET";
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
-
- Stream getImgStream = response.GetResponseStream();
- StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
- string content = reader.ReadToEnd();
- response.Close();
- reader.Dispose();
- token = content;
- }
- catch (Exception ex)
- {
-
- }
- return Ok(token);
- }
-
- /// <summary>
- /// B接口-微信小程序带参数二维码的生成
- /// </summary>
- /// <param name="access_token"></param>
- /// <returns></returns>
-
- [HttpGet]
- [Route("CreateWxCode")]
- public async Task<ActionResult> CreateWxCode(string access_token)
- {
- //适用于需要的码数量极多,或仅临时使用的业务场景
- //通过该接口生成的小程序码,永久有效,数量暂无限制。
- string postUrl = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + access_token;
-
- HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;
- request.Method = "POST";
- request.ContentType = "application/json;charset=UTF-8";
-
- string options = "{\"scene\":\"" + 1 + "\",\"page\":\"pages/home/home\",\"width\":\"150\"}";
- byte[] payload = Encoding.UTF8.GetBytes(options);
- request.ContentLength = payload.Length;
-
- Stream writer = request.GetRequestStream();
- writer.Write(payload, 0, payload.Length);
- writer.Close();
-
- System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
- System.IO.Stream stream = response.GetResponseStream();
- List<byte> bytes = new List<byte>();
- int temp = stream.ReadByte();
- while (temp != -1)
- {
- bytes.Add((byte)temp);
- temp = stream.ReadByte();
- }
- byte[] result = bytes.ToArray();
- string base64 = Convert.ToBase64String(result);//将byte[]转为base64
- var QRcode = ToImage(base64);
- CombineBitmap("D://EWM/immm/222.jpg", QRcode);
-
-
- return Ok("{\"errcode\":0,\"errmsg\":\"获取成功\",\"buffer\":\"" + base64 + "\"}");
-
- }
-
- private string ToImage(string base64String)
- {
- byte[] bytes = Convert.FromBase64String(base64String);
- using (Image image = Image.FromStream(new MemoryStream(bytes)))
- {
- var imgName = DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";
- image.Save("D://EWM/" + imgName, System.Drawing.Imaging.ImageFormat.Jpeg); // Or Png
- return "D://EWM/" + imgName;
- }
- }
-
-
- public string CombineBitmap(string backgroundImg,string FrontImg)
- {
- var imgName = DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";
- String jpg3 = "D://EWM/" + imgName;
-
- Image img1 = Image.FromFile(FrontImg);
- Image img2 = Image.FromFile(backgroundImg);
-
- int width = img2.Width;
- int height = Math.Max(img1.Height, img2.Height);
-
- Bitmap img3 = new Bitmap(width, height);
- Graphics g = Graphics.FromImage(img3);
-
- g.Clear(Color.Black);
- g.DrawImage(img2, new Point(0, 0));
- g.DrawImage(img1, new Point(img2.Width - img1.Width, img2.Height - img1.Height));
-
- System.Drawing.Text.PrivateFontCollection font = new System.Drawing.Text.PrivateFontCollection();
- font.AddFontFile("E:/Orchard_GOV/OnonMRMS/src/OrchardCore.Onon/Onon.Gov/wwwroot/Fonts/MQG.ttf");
- //检测字体类型是否可用
- var r = font.Families[0].IsStyleAvailable(FontStyle.Regular);
- var b = font.Families[0].IsStyleAvailable(FontStyle.Bold);
- //定义成新的字体对象
- FontFamily myFontFamily = new FontFamily(font.Families[0].Name, font);
- Font myFont = new Font(myFontFamily, 22, FontStyle.Bold);
- //将字体显示到控件
-
- //g.DrawString("这里输入文字", font, new SolidBrush(Color.Red), 15, 15);
- g.TranslateTransform(15, 15);
- //设置旋转角度
- g.RotateTransform(90);
- //画文字
- g.DrawString("ᠮᠣᠩᠭᠣᠯ ᠬᠥᠮᠥᠨ ᠡᠨᠳᠡ ᠪᠠᠢᠨᠠ", myFont, new SolidBrush(Color.White),60, -60);
- g.ResetTransform();
-
- g.Dispose();
-
- img2.Dispose();
- img1.Dispose();
-
- img3.Save(jpg3, System.Drawing.Imaging.ImageFormat.Jpeg);
- img3.Dispose();
- return jpg3;
- }
- -----GT
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。