.net\C#基于zxing的彩色、Logo二维码生成---随笔

1、点击项目名称右键-->管理NuGet程序包,打开NuGet包管理器窗口,进行搜索下载即可。

2、生成二维码:

 public Bitmap CreateQRCode(string str, int width, int height)
 {
     EncodingOptions options = new QrCodeEncodingOptions
      {
          DisableECI = true,
          CharacterSet = "UTF-8", //编码
          Width = width,             //宽度
           Height = height             //高度
     };

     BarcodeWriter writer = new BarcodeWriter();
     writer.Format = BarcodeFormat.QR_CODE;
     writer.Options = options;
     Bitmap map = writer.Write(str);
     return map;

 }

     


                         普通二维码效果图      


3、生成带Logo二维码

        static Bitmap GenerateLogo(string text)
        {
            //Logo 图片
            Bitmap logo = new Bitmap(@"D:\pic\34.jpg");
            //构造二维码写码器
            MultiFormatWriter writer = new MultiFormatWriter();
            Dictionary<EncodeHintType, object> hint = new Dictionary<EncodeHintType, object>();
            hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
            hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);

            //生成二维码 
            BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, 300, 300, hint);
            BarcodeWriter barcodeWriter = new BarcodeWriter();
            Bitmap map = barcodeWriter.Write(bm);


            //获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸)
            int[] rectangle = bm.getEnclosingRectangle();

            //计算插入图片的大小和位置
            int middleW = Math.Min((int)(rectangle[2] / 3.5), logo.Width);
            int middleH = Math.Min((int)(rectangle[3] / 3.5), logo.Height);
            int middleL = (map.Width - middleW) / 2;
            int middleT = (map.Height - middleH) / 2;

            //将img转换成bmp格式,否则后面无法创建Graphics对象
            Bitmap bmpimg = new Bitmap(map.Width, map.Height, PixelFormat.Format32bppArgb);
            using (Graphics g = Graphics.FromImage(bmpimg))
            {
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                g.DrawImage(map, 0, 0);
            }
            //将二维码插入图片
            Graphics myGraphic = Graphics.FromImage(bmpimg);
            //白底
            myGraphic.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH);
            myGraphic.DrawImage(logo, middleL, middleT, middleW, middleH);

            //保存成图片
            return bmpimg;
        }

                        带Logo二维码效果


 

4、生成彩色带Logo的二维码

     public Bitmap GenerateColorLogo(string text)
        {
            //Logo 图片
            Bitmap logo = new Bitmap(@"D:\pic\34.jpg");
            //构造二维码写码器
            MultiFormatWriter writer = new MultiFormatWriter();
            Dictionary<EncodeHintType, object> hint = new Dictionary<EncodeHintType, object>();
            hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
            hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            //生成二维码 
            BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, 300, 300, hint);
            BarcodeWriter barcodeWriter = new BarcodeWriter();
            Bitmap map = barcodeWriter.Write(bm);
            //获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸)
            int[] rectangle = bm.getEnclosingRectangle();
            //计算插入图片的大小和位置
            int middleW = Math.Min((int)(rectangle[2] / 3.5), logo.Width);
            int middleH = Math.Min((int)(rectangle[3] / 3.5), logo.Height);
            int middleL = (map.Width - middleW) / 2;
            int middleT = (map.Height - middleH) / 2;
          
            Bitmap bmpimg = new Bitmap(map.Width, map.Height, PixelFormat.Format32bppArgb);
            using (Graphics g = Graphics.FromImage(bmpimg))
            {
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                g.DrawImage(map, 0, 0);
            }
            //将二维码插入图片
            Graphics myGraphic = Graphics.FromImage(bmpimg);
            //白底
            myGraphic.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH);
            myGraphic.DrawImage(logo, middleL, middleT, middleW, middleH);

            Bitmap map2 = new Bitmap(bmpimg.Width, bmpimg.Height, PixelFormat.Format32bppArgb);
            Graphics graphics = Graphics.FromImage(map2);
            graphics.SmoothingMode = SmoothingMode.AntiAlias;
            graphics.DrawImage(bmpimg, 0, 0);
            map.Dispose();
            Bitmap map3 = QrCodeVertical(map2.Width, map2.Height);
            Color color = Color.FromArgb(200, 233, 0, 1);
            int num = 92;
            try
            {
                num -= (Encoding.UTF8.GetBytes(text).Length - 20) / 2;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            int num2 = num;
            int num3 = num2;
            for (int i = 0; i < map2.Width; i++)
            {
                for (int j = 0; j < map2.Height; j++)
                {
                    Color color3;
                    Color pixel = map2.GetPixel(i, j);
                    if ((i < num2) && (j < num3))
                    {
                        color3 = ((pixel.A == 0xff) && (pixel.B == 0)) ? color : pixel;
                    }
                    else
                    {
                        color3 = ((pixel.A == 0xff) && (pixel.B == 0)) ? map3.GetPixel(i, j) : pixel;

                    }
                    map2.SetPixel(i, j, color3);
                }
            }
            map3.Dispose();           
            return map2;
        }

///渐变颜色
 private Bitmap QrCodeVertical(int width, int heigth)
        {
            var image = new Bitmap(width, heigth, PixelFormat.Format32bppArgb);
            var rect = new Rectangle(0, 0, width, heigth);
            var brush = new LinearGradientBrush(rect, Color.FromArgb(230, 0x01, 0xa9, 160), Color.FromArgb(0xff, 150, 90, 0x0), LinearGradientMode.Vertical);
            Graphics graphics = Graphics.FromImage(image);
            graphics.FillRectangle(brush, rect);
            graphics.Dispose();
            return image;
        }

    带Logo的彩色二维码  


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值