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

服務(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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 虎四免费入口 | 欧美a大片 | 水野朝阳厨房系列在线观看 | 爱福利视频一区二区 | 思思久久精品在热线热 | 韩国免费特一级毛片 | 国产激情久久久久影院小草 | 我的青梅竹马是消防员2季未增删免费 | 青草视频在线观看免费资源 | 亚洲天堂精品视频 | 亚洲四虎在线 | 日韩在线一区二区三区免费视频 | 女明星放荡高h日常生活 | 91麻豆精东果冻天美传媒老狼 | 日韩精品视频福利资源站 | 亚洲午夜精品久久久久久人妖 | 好大好爽好硬 | 99热热99| 日本高清中文字幕 | 香蕉精品国产高清自在自线 | 日产国产精品亚洲系列 | 大奶喷水 | 男生的j桶女人屁免费视频 男生操男生 | 国产农村乱子伦精品视频 | 美国video | 久久99精品国产自在自线 | 国模丰满美女冰漪34d | 色综合91久久精品中文字幕 | 亚洲欧美色综合图小说 | 国产福利在线观看永久视频 | aa视频免费 | 99久久精品国产免费 | 色狠狠狠狠狠香蕉 | 国产一二三区视频 | 99在线精品免费视频九九视 | 精品国产福利在线观看一区 | 日b在线| 国产传媒天美果冻 | 女毛片 | 日韩在线天堂免费观看 | 男女真实无遮挡xx00动态图软件 |