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

//设置图片资源

        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 @ 2014-10-21 15:44  五加乘  阅读(1514)  评论(2编辑  收藏  举报