“讓客戶發展客戶”,微信海報才是微信公眾平台最高明的吸粉手段,海報上有粉絲的專屬二維碼,有粉絲的頭像及商戶宣傳的廣告等。新粉絲掃描這個專屬二維碼會關注公眾號,同時分享海報的粉絲會增加積分換取禮品或者優惠劵!本文介紹微信公眾平台下二維碼海報的開發過程。
一、微信二維碼海報介紹
微信二維碼海報是指在海報中嵌入和微信用戶關聯的參數二維碼的海報,用戶分享推廣之后,新用戶可以被統計為被推廣人員數,從而達到增加粉絲的傳播效果。其使用場景如下:






二、開發流程
在微信二維碼海報生成中,需要用到以下信息
1. 自定義菜單中設置一個菜單項,點擊后返回二維碼海報給用戶
2. 接口接收到菜單點擊之后,獲取用戶的頭像、ID(可以使用OpenID)
3. 生成和用戶關聯的參數二維碼,
4. 將參數二維碼進行縮放
5. 將頭像和參數二維碼合並成新的參數二維碼圖片
6. 將新參數二維碼圖片做為水印合成到背景海報中
7. 將用戶昵稱,以及二維碼時間戳(類型為臨時二維碼時)等文字合成到背景海報中
8. 將海報上傳成臨時圖片素材
9. 將圖片素材使用客服接口發送給用戶。
上述便是生成這張海報所需的資源及知識點,咱一步一步來哈
1、首先,模板圖片,找公司PS高手,做一個就行
2、微信帶參數二維碼的生成
#region 獲取用於換取二維碼的Ticket /// <summary> /// 獲取用於換取二維碼(臨時二維碼和長久二維碼)的Ticket /// </summary> /// <param name="senceId">場景值ID</param> /// <param name="type">值為:long 時:代表長久性二維碼 值為short時:代表臨時二維碼</param> /// <returns></returns> public static string GeterweimaTicket(int senceId, string type = "short") { string Ticket = ""; CreateQrCodeResult re = new CreateQrCodeResult(); try { if (type == "short")//臨時二維碼 { re = QrCodeApi.Create(IsExistAccess_Token2(), 604800, senceId); } else { re = QrCodeApi.Create(IsExistAccess_Token2(), 0, senceId); } Ticket = re.ticket; } catch { if (type == "short")//臨時二維碼 { Ticket = CreateTicket(IsExistAccess_Token2()); } else { Ticket = CreateLongTicket(IsExistAccess_Token2()); } } return Ticket; } #region 原方法 /// <summary> /// 創建二維碼ticket 臨時二維碼 /// </summary> /// <returns></returns> public static string CreateTicket(string TOKEN) { string result = ""; string strJson = @"{""expire_seconds"": 604800, ""action_name"": ""QR_SCENE"", ""action_info"": {""scene"": {""scene_id"": 321}}}"; string wxurl = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + TOKEN + ""; result = GetPage(wxurl, strJson); result = GetJsonValue(result, "ticket");//獲取票據 ////LogHelper.WriteLog(result); return result; } /// <summary> /// 獲取永久二維碼Ticket /// </summary> /// <param name="TOKEN"></param> /// <returns></returns> public static string CreateLongTicket(string TOKEN) { string result = ""; string strJson = @"{""action_name"": ""QR_LIMIT_SCENE"", ""action_info"": {""scene"": {""scene_id"": 123}}}"; string wxurl = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + TOKEN + ""; result = GetPage(wxurl, strJson); result = GetJsonValue(result, "ticket");//獲取票據 ////LogHelper.WriteLog(result); return result; } //{"ticket":"gQFw8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL3pFZ3lETjdsMVNDcy1DRW9PMmE3AAIEP1TcVAMECAcAAA==","expire_seconds":1800,"url":"http:\/\/weixin.qq.com\/q\/zEgyDN7l1SCs-CEoO2a7"} /// <summary> /// 通過ticket換取二維碼 /// </summary> /// <param name="TICKET">票據</param> /// <param name="openId">二維碼依照openId 命名</param> /// <param name="Pth">相對路徑 @"\weixin\HuLu\erweima2"</param> /// <returns>下載二維碼成功后的物理路徑:D:\XXXXXX.com\kkk\erweima2\201503031044566511190.jpg</returns> public static string GetTicketImage(string TICKET, string openId, string Pth) { string content = string.Empty; string strpath = string.Empty;//生成的URL 也就是 關注的URL string savepath = string.Empty;//圖片保存的路徑 string stUrl = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + HttpContext.Current.Server.UrlEncode(TICKET); HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(stUrl); req.Method = "GET"; using (WebResponse wr = req.GetResponse()) { HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse(); strpath = myResponse.ResponseUri.ToString(); WebClient mywebclient = new WebClient(); // @" savepath = HttpContext.Current.Server.MapPath(Pth) + "\\" + openId + "." + myResponse.ContentType.Split('/')[1].ToString(); // //LogHelper.WriteLog(savepath); try { mywebclient.DownloadFile(strpath, savepath);//下載生成的二維碼圖片 } catch (Exception ex) { savepath = ex.ToString(); LogHelper.WriteLog("錯誤了" + savepath); } } //LogHelper.WriteLog(savepath.ToString() + "都給我滾!"); return savepath.ToString(); } #endregion #endregion
3、獲取用戶頭像並下載:
#region 獲取用戶的詳細信息 /// <summary> /// 獲取用戶的詳細信息 /// </summary> /// <param name="REFRESH_TOKEN">Token</param> /// <param name="OPENID">用戶的OpenId</param> /// <returns>str</returns> public static Dictionary<string, object> Get_UserInfo(string REFRESH_TOKEN, string OPENID) { // Response.Write("獲得用戶信息REFRESH_TOKEN:" + REFRESH_TOKEN + "||OPENID:" + OPENID); string UserInfo = GetJson("https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + REFRESH_TOKEN + "&openid=" + OPENID); Dictionary<string, Object> obj2 = JsonConvert.DeserializeObject<Dictionary<string, Object>>(UserInfo); return obj2; } protected static string GetJson(string url) { WebClient wc = new WebClient(); wc.Credentials = CredentialCache.DefaultCredentials; wc.Encoding = Encoding.UTF8; string returnText = wc.DownloadString(url); if (returnText.Contains("errcode")) { //可能發生錯誤 } //Response.Write(returnText); return returnText; } #endregion
用戶信息用有個HeardImage 就是粉絲的頭像路徑,然后根據這個路徑,我們把它下載下來!
public class HttpDownLoad { private long fileLength; private long downLength;//已經下載文件大小,外面想用就改成公共屬性 private static bool stopDown; public HttpDownLoad() { fileLength = 0; downLength = 0; stopDown = false; // // TODO: 在此處添加構造函數邏輯 // } /// <summary> /// 文件下載 /// </summary> /// <param name="url">連接</param> /// <param name="fileName">本地保存文件名</param> public void httpDownFile(string url, string fileName) { stopDown = false; Stream str = null, fs = null; try { //獲取下載文件長度 fileLength = getDownLength(url); downLength = 0; if (fileLength > 0) { WebClient DownFile = new WebClient(); str = DownFile.OpenRead(url); //判斷並建立文件 if (createFile(fileName)) { byte[] mbyte = new byte[1024]; int readL = str.Read(mbyte, 0, 1024); fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write); //讀取流 while (readL != 0) { if (stopDown) break; downLength += readL;//已經下載大小 fs.Write(mbyte, 0, readL);//寫文件 readL = str.Read(mbyte, 0, 1024);//讀流 //progressBar.Value = (int)(downLength * 100 / fileLength); //label.Text = progressBar.Value.ToString() + "%"; //System.Windows.Forms.Application.DoEvents(); } str.Close(); fs.Close(); } } } catch (Exception ex) { if (str != null) str.Close(); if (fs != null) fs.Close(); } } /// <summary> /// 文件下載 /// </summary> /// <param name="url">連接</param> /// <param name="fileName">本地保存文件名</param> public void httpDownFile2(string url, string fileName) { try { WebClient DownFile = new WebClient(); DownFile.DownloadFile(url, fileName); } catch (Exception ex) { throw ex; } } /// <summary> /// 獲取下載文件大小 /// </summary> /// <param name="url">連接</param> /// <returns>文件長度</returns> private long getDownLength(string url) { try { WebRequest wrq = WebRequest.Create(url); WebResponse wrp = (WebResponse)wrq.GetResponse(); wrp.Close(); return wrp.ContentLength; } catch (Exception ex) { return 0; throw ex; } } /// <summary> /// 建立文件(文件如已經存在,刪除重建) /// </summary> /// <param name="fileName">文件全名(包括保存目錄)</param> /// <returns></returns> private bool createFile(string fileName) { try { if (File.Exists(fileName)) { File.Delete(fileName); } Stream s = File.Create(fileName); s.Close(); return true; } catch (Exception ex) { return false; throw ex; } } public void downClose() { stopDown = true; } }
此時:二維碼和頭像都生成了,我們就可以調用圖片合成的方法了<注意:此處需要二次圖片合成,第一次是模板和二維碼合成,第二次是把得到的新圖片和頭像再做一次合成
#region C#圖片處理 合並圖片 /// <summary> /// 調用此函數后使此兩種圖片合並,類似相冊,有個 /// 背景圖,中間貼自己的目標圖片 /// </summary> /// <param name="sourceImg">粘貼的源圖片</param> /// <param name="destImg">粘貼的目標圖片</param> /// 使用說明: string pic1Path = Server.MapPath(@"\testImg\wf.png"); /// 使用說明: string pic2Path = Server.MapPath(@"\testImg\yj.png"); /// 使用說明: System.Drawing.Image img = CombinImage(pic1Path, pic2Path); /// 使用說明:img.Save(Server.MapPath(@"\testImg\Newwf.png")); public static System.Drawing.Image CombinImage(string sourceImg, string destImg) { System.Drawing.Image imgBack = System.Drawing.Image.FromFile(sourceImg); //相框圖片 System.Drawing.Image img = System.Drawing.Image.FromFile(destImg); //照片圖片 //從指定的System.Drawing.Image創建新的System.Drawing.Graphics Graphics g = Graphics.FromImage(imgBack); g.DrawImage(imgBack, 0, 0, 148, 124); // g.DrawImage(imgBack, 0, 0, 相框寬, 相框高); g.FillRectangle(System.Drawing.Brushes.Black, 16, 16, (int)112 + 2, ((int)73 + 2));//相片四周刷一層黑色邊框 //g.DrawImage(img, 照片與相框的左邊距, 照片與相框的上邊距, 照片寬, 照片高); g.DrawImage(img, 17, 17, 112, 73); GC.Collect(); return imgBack; } #endregion
最后就是上傳至服務器並獲取MediaId和發送給粉絲了,這一塊相信大家都比較熟悉,不作多的解釋!
不懂的加我微信:18217688158 或者QQ:1936048197
