激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

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

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

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

C#生成帶二維碼的專屬微信公眾號(hào)推廣海報(bào)實(shí)例代碼

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

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

前言

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

效果如下:

C#生成帶二維碼的專屬微信公眾號(hào)推廣海報(bào)實(shí)例代碼

代碼實(shí)現(xiàn):

1.獲取臨時(shí)二維碼ticket

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/// <summary>
/// 獲取臨時(shí)二維碼ticket
/// </summary>
/// <param name="scene_str">場(chǎng)景值id openid做場(chǎng)景值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作為場(chǎng)景值的好處是通過(guò)掃a推廣的二維碼關(guān)注用戶的場(chǎng)景值便是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é)

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美精品久久久久久久久久 | 国产精品啪一品二区三区粉嫩 | 欧美ab| 久久精品一区二区三区不卡牛牛 | 免费毛片在线 | 黄色片在线免费播放 | 久久久av亚洲男天堂 | 久久99精品久久 | 中国美女一级黄色片 | 国产精品爱久久久久久久 | 久久国产精品一区 | 草久影院 | 免费在线观看国产精品 | 国色天香综合网 | 国产一区不卡 | 国产精品久久久久网站 | 深夜免费观看视频 | 一区二区三区欧美日韩 | 欧美精品一区二区三区在线播放 | 56av国产精品久久久久久久 | 中文字幕一区二区三区四区 | 91九色视频| 国产资源视频在线观看 | 极品销魂一区二区三区 | 毛片在线免费播放 | 91精品国产综合久久男男 | 国产精品久久久久久久av | 国产一区二区久久精品 | 日韩av成人 | 手机免费看一级片 | 免费大香伊蕉在人线国产 | 最新影院| 欧美精品一区二区久久久 | 亚洲最新色 | 在线免费小视频 | 亚洲xxx视频 | 久久久成人一区二区免费影院 | 午夜a狂野欧美一区二区 | 做爰xxxⅹ性护士hd在线 | 成人在线视频一区 | 国产免费乱淫av |