C#生成帶二維碼的專屬微信公眾號(hào)推廣海報(bào)實(shí)例代碼
前言
很多微信公眾號(hào)中需要生成推廣海報(bào)的功能,粉絲獲得專屬海報(bào)后可以分享到朋友圈或發(fā)給朋友,為公眾號(hào)代言邀請(qǐng)好友即可獲取獎(jiǎng)勵(lì)的。海報(bào)自帶渠道二維碼,粉絲長(zhǎng)按二維碼即可關(guān)注微信公眾號(hào),從而達(dá)到吸粉的目的。
效果如下:
代碼實(shí)現(xiàn):
1.獲取臨時(shí)二維碼ticket
/// <summary> /// 獲取臨時(shí)二維碼ticket /// </summary> /// <param name="scene_str">場(chǎng)景值ID openid做場(chǎng)景值ID</param> /// <returns></returns> public static string CreateTempQRCode(string scene_str,string access_token) { 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 + "\"}}}"); JObject jobect = (JObject)JsonConvert.DeserializeObject(result); string ticket = (string)jobect["ticket"]; if (string.IsNullOrEmpty(ticket)) { LogHelper.WriteLog(typeof(WeixinHelper), "獲取臨時(shí)二維碼ticket失敗" + result); return null; } return ticket; }
使用openid作為場(chǎng)景值的好處是通過掃A推廣的二維碼關(guān)注用戶的場(chǎng)景值便是A的openid。
2. 生成帶二維碼的專屬推廣圖片
/// <summary> /// 生成帶二維碼的專屬推廣圖片 /// </summary> /// <param name="user"></param> /// <returns></returns> public string Draw(WxUser user) { //背景圖片 string path = Server.MapPath("/Content/images/tg.jpg"); System.Drawing.Image imgSrc = System.Drawing.Image.FromFile(path); //處理二維碼圖片大小 240*240px System.Drawing.Image qrCodeImage = ReduceImage("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket="+user.ticket, 240, 240); //處理頭像圖片大小 100*100px Image titleImage = ReduceImage(user.headimgurl, 100, 100); using (Graphics g = Graphics.FromImage(imgSrc)) { //畫專屬推廣二維碼 g.DrawImage(qrCodeImage, new Rectangle(imgSrc.Width - qrCodeImage.Width - 200, imgSrc.Height - qrCodeImage.Height - 200, qrCodeImage.Width, qrCodeImage.Height), 0, 0, qrCodeImage.Width, qrCodeImage.Height, GraphicsUnit.Pixel); //畫頭像 g.DrawImage(titleImage, 8, 8, titleImage.Width, titleImage.Height); Font font = new Font("宋體", 30, FontStyle.Bold); g.DrawString(user.nickname, font, new SolidBrush(Color.Red), 110, 10); } string newpath = Server.MapPath(@"/Content/images/newtg_" + Guid.NewGuid().ToString() + ".jpg"); imgSrc.Save(newpath, System.Drawing.Imaging.ImageFormat.Jpeg); return newpath; } /// <summary> /// 縮小/放大圖片 /// </summary> /// <param name="url">圖片網(wǎng)絡(luò)地址</param> /// <param name="toWidth">縮小/放大寬度</param> /// <param name="toHeight">縮小/放大高度</param> /// <returns></returns> public Image ReduceImage(string url, int toWidth, int toHeight) { WebRequest request = WebRequest.Create(url); WebResponse response = request.GetResponse(); Stream responseStream = response.GetResponseStream(); Image originalImage = Image.FromStream(responseStream); if (toWidth <= 0 && toHeight <= 0) { return originalImage; } else if (toWidth > 0 && toHeight > 0) { if (originalImage.Width < toWidth && originalImage.Height < toHeight) return originalImage; } else if (toWidth <= 0 && toHeight > 0) { if (originalImage.Height < toHeight) return originalImage; toWidth = originalImage.Width * toHeight / originalImage.Height; } else if (toHeight <= 0 && toWidth > 0) { if (originalImage.Width < toWidth) return originalImage; toHeight = originalImage.Height * toWidth / originalImage.Width; } Image toBitmap = new Bitmap(toWidth, toHeight); using (Graphics g = Graphics.FromImage(toBitmap)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.Clear(Color.Transparent); g.DrawImage(originalImage, new Rectangle(0, 0, toWidth, toHeight), new Rectangle(0, 0, originalImage.Width, originalImage.Height), GraphicsUnit.Pixel); originalImage.Dispose(); return toBitmap; } }
3.將圖片上傳微信服務(wù)器,并發(fā)送給用戶
string imagePath = Draw(user); string result = HttpUtility.UploadFile($"https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={access_token}&type=image", imagePath); JObject jObject = (JObject)JsonConvert.DeserializeObject(result); string media_id = (string)jObject["media_id"]; if (!string.IsNullOrEmpty(media_id)) { 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>"; return resxml; } LogHelper.WriteLog(typeof(WechatController), "上傳專屬推廣圖片素材失敗" + result);
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- c# 生成二維碼的示例
- C#如何用ThoughtWorks生成二維碼
- C# 根據(jù)字符串生成二維碼的實(shí)例代碼
- C#實(shí)現(xiàn)掃描槍掃描二維碼并打印(實(shí)例代碼)
- C#基于QRCode實(shí)現(xiàn)動(dòng)態(tài)生成自定義二維碼圖片功能示例
- C#二維碼圖片識(shí)別代碼
- C#利用ZXing.Net生成條形碼和二維碼
- C#生成帶logo的二維碼
- C#生成二維碼的方法
- .NET C#利用ZXing生成、識(shí)別二維碼/條形碼
- C#利用QrCode.Net生成二維碼(Qr碼)的方法
- C#實(shí)現(xiàn)將網(wǎng)址生成二維碼圖片方法介紹
相關(guān)文章
C# 添加、修改以及刪除Excel迷你圖表的實(shí)現(xiàn)方法
下面小編就為大家分享一篇C# 添加、修改以及刪除Excel迷你圖表的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12C#實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
給大家分享用C#寫出一個(gè)計(jì)算機(jī)功能的全部代碼分享,有興趣的朋友可以跟著做一下。2018-03-03C#對(duì)DataTable里數(shù)據(jù)排序的方法
在日常開發(fā)過程中,有一個(gè)DataTable集合,里面有很多字段,現(xiàn)在要求針對(duì)某一列進(jìn)行排序,如果該列為數(shù)字的話,進(jìn)行ASC即可實(shí)現(xiàn),但是該字段類型為string,此時(shí)排序就有點(diǎn)不正確了2013-11-11