个性二维码开源专题<前背景>

//设置图片资源

复制代码
        private Image imgAgo;
        public override void SetParam()
        {
            base.SetParam();

            // 读取前背景
            string _imagePath = Environment.CurrentDirectory + @"\QrCodeModel\Images\Angry_Birds\boom.png";
            imgAgo = Image.FromFile(_imagePath);
        }
复制代码

//在生成二维码之后,加入前背景图层

复制代码
        //TODO:一系列个性二维码生成方案
        public override Bitmap Encode(string content)
        {
            try
            {
                matrix = QrCodeEncoder.calQrcode(EnCoding.GetBytes(content));
            }
            catch { throw new Exception("内容超出范围,请选择更高版本或者降低容错率"); }

            this.SetParam();

            SolidBrush Backbrush = new SolidBrush(QrCodeEncoder.QRCodeBackgroundColor);
            //SolidBrush Backbrush = new SolidBrush(Color.Transparent);//背景透明
            SolidBrush Forebrush = new SolidBrush(QrCodeEncoder.QRCodeForegroundColor);

            Bitmap image = new Bitmap(this.QrCodeW, this.QrCodeH);
            Graphics g = Graphics.FromImage(image);

            Rectangle rect = new Rectangle();

            g.FillRectangle(Backbrush, new Rectangle(0, 0, image.Width, image.Height));

            for (int i = 0; i < matrix.Length; i++)
            {
                for (int j = 0; j < matrix.Length; j++)
                {
                    rect = new Rectangle((j + this.SpacingW) * QrCodeEncoder.QRCodeScale, (i + this.SpacingH) * QrCodeEncoder.QRCodeScale, QrCodeEncoder.QRCodeScale, QrCodeEncoder.QRCodeScale);
                    if (matrix[j][i])
                    {
                        ChangeFillShape(g, Forebrush, rect, EN_FillShape.FillRectangle, new FillShape(), Backbrush);
                    }
                    else
                        ChangeFillShape(g, Backbrush, rect, EN_FillShape.FillRectangle, new FillShape(), Backbrush);
                }
            }

            //前背景
            GraphicsPath _path = ImageFix.GetWindowRegion(image, QrCodeEncoder.QRCodeForegroundColor);
            TextureBrush texture1 = new TextureBrush(new Bitmap(imgAgo, new Size(image.Width, image.Height)));
            g.FillPath(texture1, _path);

            return image;
        }
复制代码

//还可以设置,LOGO,头像等等

复制代码
        //TODO:一系列个性二维码生成方案
        public override Bitmap Encode(string content)
        {
            try
            {
                matrix = QrCodeEncoder.calQrcode(EnCoding.GetBytes(content));
            }
            catch { throw new Exception("内容超出范围,请选择更高版本或者降低容错率"); }

            this.SetParam();

            SolidBrush Backbrush = new SolidBrush(QrCodeEncoder.QRCodeBackgroundColor);
            //SolidBrush Backbrush = new SolidBrush(Color.Transparent);//背景透明
            SolidBrush Forebrush = new SolidBrush(QrCodeEncoder.QRCodeForegroundColor);

            Bitmap image = new Bitmap(this.QrCodeW, this.QrCodeH);
            Graphics g = Graphics.FromImage(image);

            Rectangle rect = new Rectangle();

            g.FillRectangle(Backbrush, new Rectangle(0, 0, image.Width, image.Height));

            for (int i = 0; i < matrix.Length; i++)
            {
                for (int j = 0; j < matrix.Length; j++)
                {
                    rect = new Rectangle((j + this.SpacingW) * QrCodeEncoder.QRCodeScale, (i + this.SpacingH) * QrCodeEncoder.QRCodeScale, QrCodeEncoder.QRCodeScale, QrCodeEncoder.QRCodeScale);
                    if (matrix[j][i])
                    {
                        ChangeFillShape(g, Forebrush, rect, EN_FillShape.FillRectangle, new FillShape(), Backbrush);
                    }
                    else
                        ChangeFillShape(g, Backbrush, rect, EN_FillShape.FillRectangle, new FillShape(), Backbrush);
                }
            }

            //Logo
            Image _logoImg = imgAgo;
            g.DrawImage(_logoImg, (this.QrCodeW - this.QrCodeW / 4) / 2, (this.QrCodeH - this.QrCodeH / 4) / 2, this.QrCodeW / 4, this.QrCodeH / 4);//插入头像

            return image;
        }
复制代码

//为了可印刷名片、图册,可以去色

            //Logo
            Image _logoImg = imgAgo;
            _logoImg = ImageFix.Blocks((Bitmap)_logoImg);
            _logoImg = ImageFix.ColorReplace((Bitmap)_logoImg, Color.Black, QrCodeEncoder.QRCodeForegroundColor);

            g.DrawImage(_logoImg, (this.QrCodeW - this.QrCodeW / 4) / 2, (this.QrCodeH - this.QrCodeH / 4) / 2, this.QrCodeW / 4, this.QrCodeH / 4);//插入头像

            return image;

 

//还可以自由配色

            //Logo
            Image _logoImg = imgAgo;
            _logoImg = ImageFix.Blocks((Bitmap)_logoImg);
            _logoImg = ImageFix.ColorReplace((Bitmap)_logoImg, Color.Black, Color.Blue);

            g.DrawImage(_logoImg, (this.QrCodeW - this.QrCodeW / 4) / 2, (this.QrCodeH - this.QrCodeH / 4) / 2, this.QrCodeW / 4, this.QrCodeH / 4);//插入头像

 

 以下是开源地址,国外github,国内oschina.net

oschina.net:

http://git.oschina.net/cheng5x/Yc.QrCode

 

github:

https://github.com/cheng5x/YcQrCode

 

官方网站:
http://original-ad.com

码晒客讨论QQ群:
28629273

posted @   五加乘  阅读(1521)  评论(2编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
点击右上角即可分享
微信分享提示