這篇文章主要介紹了C#如何生成帶二維碼的專屬微信公眾號(hào)推廣海報(bào),具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)從2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元瑞金做網(wǎng)站,已為上家服務(wù),為瑞金各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108效果如下:
代碼實(shí)現(xiàn):
1.獲取臨時(shí)二維碼ticket
/// <summary> /// 獲取臨時(shí)二維碼ticket /// </summary> /// <param name="scene_str">場景值ID openid做場景值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作為場景值的好處是通過掃A推廣的二維碼關(guān)注用戶的場景值便是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);
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“C#如何生成帶二維碼的專屬微信公眾號(hào)推廣海報(bào)”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司,關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、網(wǎng)站設(shè)計(jì)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
當(dāng)前標(biāo)題:C#如何生成帶二維碼的專屬微信公眾號(hào)推廣海報(bào)-創(chuàng)新互聯(lián)
地址分享:http://muchs.cn/article18/cdsdgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、品牌網(wǎng)站設(shè)計(jì)、做網(wǎng)站、用戶體驗(yàn)、網(wǎng)站收錄、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容