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


    //设置图片资源

            private Image img1;
            public override void SetParam()
            {
                base.SetParam();
    
                //读取图片                                             
                string _imagePath1 = Environment.CurrentDirectory + @"imagepoint2.jpg";
                img1 = Image.FromFile(_imagePath1);
            }

    //首先设置背景透明

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

    //先插入背景图,后生成二维码

            //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);
    
                //后背景
                g.DrawImage(img1, 0, 0, this.QrCodeW, this.QrCodeH);
    
    
                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(), Forebrush);
                        }
                        else
                        {
                            ChangeFillShape(g, Backbrush, rect, EN_FillShape.FillRectangle, new FillShape(), Backbrush);
                        }
                    }
                }
                return image;
            }

    //当然,还可以使用一些图片处理效果,例如:平铺

            //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);
    
                //平铺
                GraphicsPath _path = ImageFix.GetWindowRegion(image, QrCodeEncoder.QRCodeForegroundColor);
                TextureBrush texture1 = new TextureBrush(img1);
                g.FillPath(texture1, _path);
    
                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(), Forebrush);
                        }
                        else
                        {
                            ChangeFillShape(g, Backbrush, rect, EN_FillShape.FillRectangle, new FillShape(), Backbrush);
                        }
                    }
                }
                return image;
            }

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

    oschina.net:

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

    github:

    https://github.com/cheng5x/YcQrCode

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

    码晒客讨论QQ群:
    28629273

  • 相关阅读:
    leetcode -- Maximum Depth of Binary Tree
    leetcode -- Binary Tree Level Order Traversal
    leetcode -- Binary Tree Inorder Traversal
    leetcode -- Subsets II
    Egret入门学习日记 --- 第十七篇(书中 7.4~8.2节 内容)
    Egret入门学习日记 --- 问题汇总
    Egret入门学习日记 --- 第六篇(书中 3.6~3.9节 内容)
    Egret入门学习日记 --- 第十六篇(书中 6.10~7.3节 内容)
    Egret入门学习日记 --- 第十五篇(书中 6.1~6.9节 内容)
    Egret入门学习日记 --- 第十四篇(书中 5.4~5.6节 内容)
  • 原文地址:https://www.cnblogs.com/cheng5x/p/4384225.html
Copyright © 2020-2023  润新知