C#生成帶二維碼的專屬微信公眾號推廣海報實(shí)例代碼
前言
很多微信公眾號中需要生成推廣海報的功能,粉絲獲得專屬海報后可以分享到朋友圈或發(fā)給朋友,為公眾號代言邀請好友即可獲取獎勵的。海報自帶渠道二維碼,粉絲長按二維碼即可關(guān)注微信公眾號,從而達(dá)到吸粉的目的。
效果如下:
代碼實(shí)現(xiàn):
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推廣的二維碼關(guān)注用戶的場景值便是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">圖片網(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ā)送給用戶
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í)價值,如果有疑問大家可以留言交流,謝謝大家對我們的支持。
上一篇:c#中WebService的介紹及調(diào)用方式小結(jié)
欄 目:C#教程
下一篇:C# 6.0 內(nèi)插字符串(Interpolated Strings )的使用方法
本文標(biāo)題:C#生成帶二維碼的專屬微信公眾號推廣海報實(shí)例代碼
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/4996.html
您可能感興趣的文章
- 01-10C# readnodefile()不能讀取帶有文件名為漢字的osg文件解決方法
- 01-10C#獲取動態(tài)生成的CheckBox值
- 01-10基于C#實(shí)現(xiàn)簡單離線注冊碼生成與驗(yàn)證
- 01-10C#中實(shí)現(xiàn)一次執(zhí)行多條帶GO的sql語句實(shí)例
- 01-10C#二維數(shù)組基本用法實(shí)例
- 01-10C#實(shí)現(xiàn)帶消息數(shù)的App圖標(biāo)
- 01-10基于C#生成條形碼操作知識匯總附源碼下載
- 01-10使用C#發(fā)送帶附件的電子郵件的方法的代碼示例分析
- 01-10C#實(shí)現(xiàn)帶進(jìn)度條的ListView
- 01-10詳解C#設(shè)計模式編程中生成器模式的使用


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文


