前言
很多微信公眾號中需要生成推廣海報的功能,粉絲獲得專屬海報后可以分享到朋友圈或發(fā)給朋友,為公眾號代言邀請好友即可獲取獎勵的。海報自帶渠道二維碼,粉絲長按二維碼即可關注微信公眾號,從而達到吸粉的目的。
效果如下:
代碼實現(xiàn):
1.獲取臨時二維碼ticket
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
/// <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. 生成帶二維碼的專屬推廣圖片
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
/// <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">圖片網絡地址</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.將圖片上傳微信服務器,并發(fā)送給用戶
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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); |
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:https://www.cnblogs.com/gracexu/p/10081111.html