亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

前言

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

效果如下:

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

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

 /// <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)景值的好處是通過掃A推廣的二維碼關(guān)注用戶的場(chǎng)景值便是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é)

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

相關(guān)文章

  • 實(shí)例詳解C#正則表達(dá)式

    實(shí)例詳解C#正則表達(dá)式

    這篇文章主要通過實(shí)例詳解C#正則表達(dá)式的相關(guān)資料,需要的朋友可以參考下
    2016-01-01
  • 驗(yàn)證碼的三個(gè)常見漏洞和修復(fù)方法

    驗(yàn)證碼的三個(gè)常見漏洞和修復(fù)方法

    這篇文章主要介紹了驗(yàn)證碼的三個(gè)常見漏洞和修復(fù)方法,本文講解了把驗(yàn)證碼存儲(chǔ)在Cookie中、沒有進(jìn)行非空判斷、沒有及時(shí)銷毀驗(yàn)證碼三個(gè)常見問題和解決方法,需要的朋友可以參考下
    2015-03-03
  • C#自定義針對(duì)URL地址的處理類實(shí)例

    C#自定義針對(duì)URL地址的處理類實(shí)例

    這篇文章主要介紹了C#自定義針對(duì)URL地址的處理類,實(shí)例分析了C#解析URL地址實(shí)現(xiàn)URl參數(shù)加密解密及參數(shù)解析的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-03-03
  • C# 添加、修改以及刪除Excel迷你圖表的實(shí)現(xiàn)方法

    C# 添加、修改以及刪除Excel迷你圖表的實(shí)現(xiàn)方法

    下面小編就為大家分享一篇C# 添加、修改以及刪除Excel迷你圖表的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • C#實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能

    C#實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能

    給大家分享用C#寫出一個(gè)計(jì)算機(jī)功能的全部代碼分享,有興趣的朋友可以跟著做一下。
    2018-03-03
  • C#對(duì)DataTable里數(shù)據(jù)排序的方法

    C#對(duì)DataTable里數(shù)據(jù)排序的方法

    在日常開發(fā)過程中,有一個(gè)DataTable集合,里面有很多字段,現(xiàn)在要求針對(duì)某一列進(jìn)行排序,如果該列為數(shù)字的話,進(jìn)行ASC即可實(shí)現(xiàn),但是該字段類型為string,此時(shí)排序就有點(diǎn)不正確了
    2013-11-11
  • C#中變量、常量、枚舉、預(yù)處理器指令知多少

    C#中變量、常量、枚舉、預(yù)處理器指令知多少

    這篇文章主要介紹了c#共有其中變量類型有:靜態(tài)變量、實(shí)類變量、數(shù)組元素、數(shù)值參數(shù)、引用參數(shù)、輸出參數(shù)和局部變量,需要的朋友可以參考一下
    2017-04-04
  • C# for循環(huán)的經(jīng)典案例集錦

    C# for循環(huán)的經(jīng)典案例集錦

    本篇文章主要介紹了關(guān)于for循環(huán)的經(jīng)典案例,具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-05-05
  • C#中Dapper的使用教程

    C#中Dapper的使用教程

    Dapper是一款輕量級(jí)ORM工具(Github),Dapper語法十分簡(jiǎn)單。并且無須遷就數(shù)據(jù)庫的設(shè)計(jì),今天通過本文給大家介紹C# Dapper的使用,感興趣的朋友一起看看吧
    2021-07-07
  • C#實(shí)現(xiàn)同步模式下的端口映射程序

    C#實(shí)現(xiàn)同步模式下的端口映射程序

    這篇文章介紹了C#實(shí)現(xiàn)同步模式下的端口映射程序,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06

最新評(píng)論