一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - C# - C#生成帶二維碼的專屬微信公眾號推廣海報實例代碼

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

2022-03-06 13:31指間人生 C#

這篇文章主要給大家介紹了關(guān)于利用C#如何生成帶二維碼的專屬微信公眾號推廣海報的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們一起來看看吧

前言

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

效果如下:

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

代碼實現(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推廣的二維碼關(guān)注用戶的場景值便是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">圖片網(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ā)送給用戶

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

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對服務(wù)器之家的支持。

原文鏈接:https://www.cnblogs.com/gracexu/p/10081111.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲haose在线观看 | 亚洲 欧美 国产 综合 播放 | 91粉色视频在线导航 | 精品视频入口 | 四虎永久在线精品免费影视 | 亚洲国产成人久久99精品 | 污小说免费 | 男同桌扒开女同桌胸罩喝奶 | 天天干夜夜玩 | 久久亚洲一级α片 | 色婷婷在线视频 | 爆操美女 | 小小水蜜桃视频高清在线播放 | 欧美伊人久久久久久久久影院 | 欧美专区亚洲 | 91精品国产91久久久久久 | 国产这里有精品 | 国产在线xvideos | 99热精品国产麻豆 | 双子母性本能在线 | 日韩欧美一区二区三区免费看 | 午夜性色一区二区三区不卡视频 | 亚洲精品视频久久 | 2020最新版的ab片 | 扒开大腿狠狠挺进视频 | 欧美穿高跟鞋做爰 | 女仆掀起蕾丝裙被打屁股作文 | 青草青草视频 | 国产精品调教 | 亚洲国产AV无码综合在线 | 2021国产麻豆剧传媒剧情动漫 | 天堂成人影院 | 手机在线观看网站免费视频 | 俄罗斯freeⅹ性欧美 | 精品国产在线观看 | 国色天香 社区视频 | 亚洲国产欧美在线人网站 | 日韩高清在线免费观看 | 大肥臀风间由美 中文字幕 大东北chinesexxxx露脸 | 日韩精品福利视频一区二区三区 | 日韩一区国产二区欧美三 |