简介
本案例适用在市场部同事做推广营销时推送个人专属链接,绑定自身专属客户,引导客户了解产品等各方面业务的一种引导模式。
框架环境介绍
- 控制台应用程序
- .net framework v4.5.2
- 组件 qrcoder
- vs2019
创建项目
使用vs创建控制台应用程序,框架版本可以根据自己实际情况选择,
引用与其框架对应不冲突版本组件qrcoder
右键项目 吊起nuget引用资源窗口,在浏览处搜索qrcoder插件
准备业务场景所需要的东西
在项目\bin\debug下新建四个文件夹,dingwei 海报图模板 ,logo 二维码logo,new 生成的海报,
user 生成的二维码logo临时存储区
业务代码
using qrcoder; using system; using system.collections.generic; using system.drawing; using system.drawing.imaging; using system.linq; using system.text; using system.threading.tasks; namespace qrcoder_img { class program { static void main(string[] args) { //业务场景:本次是通过一个表单带上市场部员工id //生成二维码,并定位到海报模板上,随后绑定到该 //员工,此刻该员工就拥有自己的推广二维码 //假设 员工id ,真实业务场景应从库中抓取未生成二维码的员工id,定时作业执行。 var id = 1; // 要生成的二维码链接 var url="http://baidu.com?id="+id; //生成的二维码路径 string ewm = ""; //生成二维码,并返回路径 renderqrcode(url, id, ref ewm); //定位生成新的图 image img = image.fromfile(ewm); image imgback = image.fromfile(system.io.directory.getcurrentdirectory() + "/dingwei/1.png"); bitmap bmp = combinimage(270, 270, imgback, img, 227, 668); //最终生成图的路径,可在此之后同步数据库,或者其他不同形式业务 string str = system.io.directory.getcurrentdirectory() + "/new/1tg1.png"; if (system.io.file.exists(str)) system.io.file.delete(str); bmp.save(str, imageformat.png); bmp.clone(); bmp.dispose(); img.dispose(); system.io.file.delete(ewm); } /// <summary> /// 生成带有logo的二维码 /// </summary> /// <param name="url">二维码链接</param> /// <param name="id">市场部用户id,也作为生成图的名字标识</param> /// <param name="ewm">生成图的路径</param> public static void renderqrcode(string url, int id, ref string ewm) { string level = "l"; qrcodegenerator.ecclevel ecclevel = (qrcodegenerator.ecclevel)(level == "l" ? 0 : level == "m" ? 1 : level == "q" ? 2 : 3); qrcodegenerator qrgenerator = new qrcodegenerator(); qrcodedata qrcodedata = qrgenerator.createqrcode(url, ecclevel); qrcode qrcode = new qrcode(qrcodedata); var imageewm = qrcode.getgraphic(20, color.black, color.white, geticonbitmap(system.io.directory.getcurrentdirectory() + "/logo/appicon.png")); string str = system.io.directory.getcurrentdirectory() + "/user/user" + id + ".jpg"; if (system.io.file.exists(str)) system.io.file.delete(str); imageewm.save(str, imageformat.jpeg); imageewm.clone(); imageewm.dispose(); qrcode.dispose(); qrcodedata.dispose(); ewm = str; } public static bitmap geticonbitmap(string url) { bitmap img = null; if (url.length > 0) { try { img = new bitmap(url); } catch (exception) { } } return img; } /// <summary> /// 两张图合并 /// </summary> /// <param name="width">二维码绘画大小</param> /// <param name="height">二维码绘画大小</param> /// <param name="imgback">模板图</param> /// <param name="img">二维码图</param> /// <param name="xdeviation">定位x</param> /// <param name="ydeviation">定位y</param> /// <returns></returns> public static bitmap combinimage(int width, int height, image imgback, image img, int xdeviation = 0, int ydeviation = 0) { bitmap bmp = new bitmap(imgback.width, imgback.height); graphics g = graphics.fromimage(bmp); g.clear(color.white); g.drawimage(imgback, 0, 0, imgback.width, imgback.height); g.drawimage(img, xdeviation, ydeviation, width, height); gc.collect(); g.dispose(); return bmp; } } }
最终效果
到此这篇关于c#使用qrcode生成海报图并嵌入定位带logo的二维码的文章就介绍到这了,更多相关c# qrcode生成海报图内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论