C# 实现生成带二维码的专属微信公众号推广海报
很多微信公众号中需要生成推广海报的功能,粉丝获得专属海报后可以分享到朋友圈或发给朋友,为公众号代言邀请好友即可获取奖励的。海报自带渠道二维码,粉丝长按二维码即可关注微信公众号,从而达到吸粉的目的。
效果如下:
代码实现:
1.获取临时二维码ticket
/// <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. 生成带二维码的专属推广图片
/// <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.将图片上传微信服务器,并发送给用户
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);
详细请查看 http://blog.yshizi.cn/50.html
C# 实现生成带二维码的专属微信公众号推广海报的更多相关文章
- 为什么下载APP,扫描二维码,关注微信公众号,就会送牛奶送小礼品?下载使用量高,会怎样?
以前的老办法是到处贴广告,电视上,广播上各种宣传. 在互联网时代,企业要盈利,除了不断优化升级自己的产品和服务,大量推广宣传产品,还要懂得用户思维.现在有网站,有APP,有微信,有二维码,可以卖产品, ...
- 通过生成支付二维码来实现微信支付的解决方案 - EasyWechat版(转)
上一篇我们讲了在微信浏览器内实现微信支付的功能,它特别适合于一些基于微信公众号的h5站点等,支付流程也相当流畅,但是... 还有一种情况,比如现在北哥兄弟连PC版,是生成了一个二维码,这个二维码是专属 ...
- Android之扫描二维码和根据输入信息生成名片二维码
开发中常常遇到二维码扫码操作,前段时间做项目要实现该功能,于是网上查找资料实现了,现在把他做出来给各位分享一下,主要包含了二维码扫描和生成二维码名片. 先来看看效果图: 生成的二维码,打开微信扫一 ...
- iOS开发——iOS7(及以后版本) SDK自带二维码(含条形码)扫码、二维码生成
本文转载至 http://www.cnblogs.com/leotangcn/p/4357907.html 现在很多APP都涉及了二维码扫码功能,这个功能简单实用,很多情况下用户乐于使用,现在本文带来 ...
- 公司开发的APP,如何生成一个二维码,供客户下载使用
1.其实和简单,因为一般的用户使用扫一扫,大多数都是用微信自带的扫一扫工具 而,微信打开的二维码页面,会自动屏蔽apk文件,所以显然把apk的url生成一个二维码,让用户扫一扫就能直接下载,这样是行不 ...
- Android生成自定义二维码
前面说过两种二维码扫描方式,现在说如何生成自定义酷炫二维码.二维码生成需要使用Google开源库Zxing,Zxing的项目地址:https://github.com/ZBar/ZBar,我们只需要里 ...
- python小工具myqr生成动态二维码
python小工具myqr生成动态二维码 (一)安装 (二)使用 (一)安装 命令: pip install myqr 安装完成后,就可以在命令行中输入 myqr 查看下使用帮助: myqr --he ...
- Python | 一行命令生成动态二维码
当我看到别人的二维码都做的这么炫酷的时候,我心动了! 我也想要一个能够吸引眼球的二维码,今天就带大家一起用 Python 来做一个炫酷的二维码! 首先要安装工具 myqr: pip install m ...
- PHP 使用GD库合成带二维码的海报步骤以及源码实现
PHP 使用GD库合成带二维码的海报步骤以及源码实现 在做微信项目开发过程中,经常会遇到图片合成的问题,比如将用户的二维码合成到宣传海报中,那么,遇到这种情况,利用PHP的GD库也是很容易实现的,实现 ...
随机推荐
- Seal Report开放数据库报表工具(.Net)
Seal Report_20160923 概述:开放数据库报表工具(.Net) 简介:Seal-Report提供了一个完整的框架,用于从任何数据库生成日常报告和仪表板.Seal-Report是Micr ...
- Redis Cluster(Redis 3.X)设计要点
Redis 3.0.0 RC1版本号10.9号公布,Release Note这个版本号支持Redis Cluster.相信非常多同学期待已久,只是这个版本号仅仅是RC版本号,要应用到生产环境,还得等等 ...
- HDU 1496 Equations hash HDU上排名第一!
看题传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1496 题目大意: 给定a,b,c,d.a*x1^2+b*x2^2+c*x3^2+d*x4^2=0 ...
- 【例题5-2 UVA - 101】The Blocks Problem
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用vector模拟就好. resize的时候,只是把多余的清理掉. 原先的不会变的. [错的次数] 在这里输入错的次数 [反思] 在 ...
- jquery-12 jquery中的工具方法有哪些
jquery-12 jquery中的工具方法有哪些 一.总结 一句话总结:四个较常用方法.1.isArray();2.isFunction();3.isEmptyObejct();4.trim(); ...
- PatentTips - Multi-host SATA Controller
BACKGROUND The present subject matter relates, in general, to a computing system having multi-host p ...
- 【2186】Popular Cows(强连通分支及其缩点)
id=2186">[2186]Popular Cows(强联通分支及其缩点) Popular Cows Time Limit: 2000MS Memory Limit: 65536 ...
- maven pom.xml基本使用方法
pom.xml文件是Maven进行工作的主要配置文件.在这个文件里我们能够配置Maven项目的groupId.artifactId和version等Maven项目必须的元素:能够配置Maven项目须要 ...
- php面试题7(1、unset变量是删除栈变量,并不删除堆变量)(2、php爬虫特别简单: 可以file_get_contents和直接fopen)
php面试题7(1.unset变量是删除栈变量,并不删除堆变量)(2.php爬虫特别简单: 可以file_get_contents和直接fopen) 一.总结 1.unset变量是删除栈变量,并不删除 ...
- 号外:小雷将开发一款Java版的简易CMS系统
我的个人官网: http://FansUnion.cn 已经改版,隆重上线了,欢迎关注~持续升级中... 出于个人兴趣.技术总结.工作相关,我终于想要做一个简单的CMS系统了. 原来想研究,D ...