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


    //设置图片资源

            private Image imgAgo;
            public override void SetParam()
            {
                base.SetParam();
    
                // 读取前背景
                string _imagePath = Environment.CurrentDirectory + @"QrCodeModelImagesAngry_Birdsoom.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

  • 相关阅读:
    滴滴Ceph分布式存储系统优化之锁优化
    滴滴数据仓库指标体系建设实践
    滴滴AI Labs斩获国际机器翻译大赛中译英方向世界第三
    滴滴数据通道服务演进之路
    可编程网卡芯片在滴滴云网络的应用实践
    GPU虚拟机创建时间深度优化
    滴滴ElasticSearch千万级TPS写入性能翻倍技术剖析
    使用zip()并行迭代
    循环结构
    选择结构
  • 原文地址:https://www.cnblogs.com/cheng5x/p/4040546.html
Copyright © 2020-2023  润新知