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

//设置图片资源

        private Image img1;
        public override void SetParam()
        {
            base.SetParam();

            //读取图片                                             
            string _imagePath1 = Environment.CurrentDirectory + @"\image\point2.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

posted @ 2015-04-01 16:14  五加乘  阅读(1159)  评论(0编辑  收藏  举报