C# 實現生成帶二維碼的專屬微信公眾號推廣海報

原文:C# 實現生成帶二維碼的專屬微信公眾號推廣海報

很多微信公眾號中需要生成推廣海報的功能,粉絲獲得專屬海報后可以分享到朋友圈或發給朋友,為公眾號代言邀請好友即可獲取獎勵的。海報自帶渠道二維碼,粉絲長按二維碼即可關注微信公眾號,從而達到吸粉的目的。

效果如下:

微信圖片_20181206210040.jpg

代碼實現:

1.獲取臨時二維碼ticket

    /// <summary>/// 獲取臨時二維碼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), "獲取臨時二維碼ticket失敗" + result);return null;}return ticket;}

使用openid作為場景值的好處是通過掃A推廣的二維碼關注用戶的場景值便是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*240pxSystem.Drawing.Image qrCodeImage = ReduceImage("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket="+user.ticket, 240, 240);//處理頭像圖片大小 100*100pxImage 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">圖片網絡地址</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.將圖片上傳微信服務器,并發送給用戶

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);

詳細請查看 http://blog.yshizi.cn/50.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/450050.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/450050.shtml
英文地址,請注明出處:http://en.pswp.cn/news/450050.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Android應用開發—PendingIntent:如何判斷兩個PendingIntent對等

摘錄自&#xff1a;PendingIntent詳解 如何判斷兩個PendingIntent對等&#xff1a; 兩個PendingIntent對等是指它們的operation一樣, 且其它們的Intent的action, data, categories, components和flags都一樣。但是它們的Intent的Extra可以不一樣。 主要常量 FLAG_CANCEL_CUR…

最新研究顯示人類智力逐漸下降且變得更愚蠢

請大家去測試智力&#xff0c;看看是否正常水平&#xff1f;真的&#xff0c;智力在下降&#xff0c;在網絡、電子設備和化肥、農藥&#xff0c;轉基因等因素下&#xff0c;人越來越差了。 保存到相冊研究人員稱現代人類將逐漸智力下降&#xff0c;這是因為我們不再需要智力來維…

缺陷的背后---LIMIT M,N 分頁查找

一、問題發現篇 最近組內做了一次典型缺陷分享時&#xff0c;翻閱2018年的缺陷&#xff0c;找到了一個讓我覺得“有料”的bug&#xff08;別的同事測試發現的&#xff09;&#xff0c;先大致簡單的描述下這個問題&#xff1a; 需要實現的功能&#xff1a;從一個DB庫同步某一段時…

Android應用開發—Intent組件詳解

轉載自&#xff1a;Android中Intent組件詳解 Intent是不同組件之間相互通訊的紐帶&#xff0c;封裝了不同組件之間通訊的條件。 Intent本身是定義為一個類別(Class)&#xff0c;一個Intent對象表達一個目的(Goal)或期望(Expectation)&#xff0c;敘述其所期望的服務或動作、與…

angularjs 結構的兩種寫法(2)

app.js里面 route.js 本項目中的路由寫法,路由的意思是&#xff1a;對應的跳轉頁面路徑&#xff0c;比如此處當路由是member.user-statisttic時&#xff0c;是會跳轉到url&#xff1a;http://.../user-statisttic.html頁面&#xff0c;然后此頁面對應的ctrl會解析也頁面的參數。…

Linux指令小記(簡明實用)

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 1、ls指令,用于列出當前目錄的文件 通過添加-l參數可以使文件以詳情模式列出 通過添加-a參數可以將包含隱藏文件在內的全部文件列出。…

python之glob的用法

目錄 獲取特定擴展名的所有文件 獲取特定目錄下的所有文件 遞歸獲取所有文件 轉義特殊字符 iglob glob 是 Python 中用于文件模式匹配的一個模塊。它使用 Unix shell-style 的通配符來進行匹配&#xff0c;并返回所有匹配的文件路徑列表。 下面是一些 glob 的基本用法&am…

設置Eclipse中的字符集為UTF-8

Eclipse 修改字符集 默認情況下 Eclipse 字符集為 GBK&#xff0c;但現在很多項目采用的是 UTF-8&#xff0c;這是我們就需要設置我們的 Eclipse 開發環境字符集為 UTF-8&#xff0c; 設置步驟如下&#xff1a; 在菜單欄選擇 Window -> Preferences -> General -> Wor…

換位思考的最高境界是換待遇,所以,換位思考就是空話!!!

換位思考的最高境界是換待遇&#xff0c;所以&#xff0c;換位思考就是空話&#xff01;&#xff01;&#xff01; 換位思考是件說難也難&#xff0c;說容易也容易的事情。 如果你肯把你的工資待遇財富地位跟李彥宏互換一下&#xff0c;你就可以輕松理解他到底為什么非要在百度…

一個記錄最近搜索歷史的LRU實現

對于很多有搜索需求的功能&#xff0c;一般需要展示下最近n次的歷史搜索記錄&#xff0c;主要有以下幾個功能點&#xff1a; 最近搜索條目放在最前面&#xff0c;最早的搜索記錄放在最后。只記錄最近n條數據&#xff0c;如果超過n條搜索記錄&#xff0c;刪除搜索時間最久遠的記…

Python數據可視化2018:數據可視化庫為什么這么多?

本文最初發布于Anaconda開發者博客&#xff0c;經原作者授權由InfoQ中文站翻譯并分享。 在奧斯汀舉行的SciPy 2018年特別會議上&#xff0c;大量開源Python可視化工具的代表分享了他們對Python數據可視化未來的展望。我們看到了Matplotlib、Plotly、VisPy等許多庫的更新。我作為…

Oulipo (KMP出現次數)

The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter e. He was a member of the Oulipo group. A quote from the book: Tout avait Pair normal, mais tout s’affirmait faux. Tout avait Fair normal, d’abord, puis su…

從夫妻吵架中看項目管理

從夫妻吵架中看項目管理&#xff08;很有意思的文章&#xff09; 首先要說明&#xff1a;和老婆吵架無論原因如何&#xff0c;無論結果如何你都是錯的&#xff0c;老婆永遠是對的。但是我不是神仙&#xff0c;偶爾也要吵架。但是如何讓吵架也發揮作用&#xff0c;增進夫妻感情&…

SpringMVC工作原理

大家好&#xff0c;我是IT修真院深圳分院第十一期學員&#xff0c;一枚正直純潔善良的JAVA程序員。 今天給大家分享一下&#xff0c;修真院官網JAVA任務二的一個知識點&#xff1a;SpringMVC工作原理 1、背景介紹 一&#xff1a;背景介紹 JavaWeb經歷的幾個變化&#xff1a; 1:…

Android應用開發—如何解決handler的警告:Handler Class Should be Static or Leaks Occur

轉自android handler的警告Handler Class Should be Static or Leaks Occur 在使用Handler更新UI的時候&#xff0c;我是這樣寫的&#xff1a; public class SampleActivity extends Activity {private final Handler mLeakyHandler new Handler() {Overridepublic void hand…

從遠程(包括ftp,http等協議)地址獲取文件流信息

URL url new URL("ftp://172.18.251.155:8010/recordsImg/2019-01-28/000008_1548649813267.jpg"); MultipartFile multipartFile new MockMultipartFile(fileName,fileName,"", url.openStream());轉載于:https://www.cnblogs.com/baihaojie/p/10331134…

shell 數組

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 1&#xff09;定義數組&#xff1a; my_array(1 2 3 4) 也可這樣賦值&#xff1a;my_array[4]愛 讀取&#xff1a; echo ${my_array[2]…

nodejs 實現文件拷貝

通過4中不通的方式實現對一個文件的拷貝 方式一&#xff1a;readFile 異步一次性讀取文件再寫入 //異步形式讀取文件 function copyFile(url){const extName path.extname(url)const fileName path.basename(url)const dirName path.dirname(url)fs.readFile(url, (err, dat…

國家部委對4G調研:未定給中電信聯通發放牌照

一場有關4G牌照發放的論戰正在發酵&#xff0c;矛盾的核心在于&#xff0c;除了中移動外&#xff0c;政府是否也會向中電信和聯通發放TD-LTE(中國主導的4G標準)牌照 記者 王云輝 雍忠瑋 一場圍繞4G的新博弈已經白熱化。 “多個國家部委正在對4G展開全面調研&#xff0c;但最終如…