网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

C#如何生成带二维码的专属微信公众号推广海报-创新互联

这篇文章主要介绍了C#如何生成带二维码的专属微信公众号推广海报,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

创新互联从2013年开始,是专业互联网技术服务公司,拥有项目成都网站设计、网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元瑞金做网站,已为上家服务,为瑞金各地企业和个人服务,联系电话:18982081108

效果如下:

C#如何生成带二维码的专属微信公众号推广海报

代码实现:

1.获取临时二维码ticket

 /// 
 /// 获取临时二维码ticket
 /// 
 /// 场景值ID openid做场景值ID
 /// 
 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. 生成带二维码的专属推广图片

 /// 
 /// 生成带二维码的专属推广图片
 /// 
 /// 
 /// 
 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;
 }


 /// 
 /// 缩小/放大图片
 /// 
 /// 图片网络地址
 /// 缩小/放大宽度
 /// 缩小/放大高度
 /// 
 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 = "" + nowtime + "";
  return resxml;
}
LogHelper.WriteLog(typeof(WechatController), "上传专属推广图片素材失败" + result);

感谢你能够认真阅读完这篇文章,希望小编分享的“C#如何生成带二维码的专属微信公众号推广海报”这篇文章对大家有帮助,同时也希望大家多多支持创新互联成都网站设计公司,关注创新互联成都网站设计公司行业资讯频道,更多相关知识等着你来学习!

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、网站设计器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


文章名称:C#如何生成带二维码的专属微信公众号推广海报-创新互联
文章源于:http://bjjierui.cn/article/cdsdgp.html

其他资讯