当前位置: 代码网 > it编程>编程语言>C# > C#使用QRCode生成海报图并嵌入定位带logo的二维码

C#使用QRCode生成海报图并嵌入定位带logo的二维码

2024年05月18日 C# 我要评论
简介本案例适用在市场部同事做推广营销时推送个人专属链接,绑定自身专属客户,引导客户了解产品等各方面业务的一种引导模式。框架环境介绍控制台应用程序.net framework v4.5.2组件 qrc

简介

本案例适用在市场部同事做推广营销时推送个人专属链接,绑定自身专属客户,引导客户了解产品等各方面业务的一种引导模式。

框架环境介绍

  • 控制台应用程序
  • .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生成海报图内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com