码迷,mamicode.com
首页 > Windows程序 > 详细

winform生成条形码和二维码(ZXing.Net)

时间:2018-10-30 10:17:33      阅读:358      评论:0      收藏:0      [点我收藏+]

标签:domain   type   oid   大小   object   代码   odi   system   rate   

首先在项目添加ZXing.Net。

工具-->Nuget包管理器-->Nuget程序包  在所搜栏输入 ZXing.Net 如下图:技术分享图片

技术分享图片

 

添加完成后会看见:

技术分享图片

效果图:

技术分享图片

 

所有代码:

    /// <summary>
        /// 条形码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            System.Drawing.Image image;
            int width = 117, height = 55;
            string fileSavePath = AppDomain.CurrentDomain.BaseDirectory + "TXM.jpg";
            if (File.Exists(fileSavePath))
                File.Delete(fileSavePath);
            GetBarcode(height, width, BarcodeLib.TYPE.CODE128, DateTime.Now.ToString("yyyyMMddHHMMss"), out image, fileSavePath);
            pictureBox1.Image = Image.FromFile("TXM.jpg");
        }

        /// <summary>
        /// 条形码
        /// </summary>
        /// <param name="height"></param>
        /// <param name="width"></param>
        /// <param name="type"></param>
        /// <param name="code"></param>
        /// <param name="image"></param>
        /// <param name="fileSaveUrl"></param>
        public static void GetBarcode(int height, int width, BarcodeLib.TYPE type, string code, out System.Drawing.Image image, string fileSaveUrl)
        {
            try
            {
                image = null;
                BarcodeLib.Barcode b = new BarcodeLib.Barcode();
                b.BackColor = System.Drawing.Color.White;//图片背景颜色
                b.ForeColor = System.Drawing.Color.Black;//条码颜色
                b.IncludeLabel = true;
                b.Alignment = BarcodeLib.AlignmentPositions.LEFT;
                b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER;//code的显示位置
                b.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;//图片格式
                System.Drawing.Font font = new System.Drawing.Font("verdana", 10f);//字体设置
                b.LabelFont = font;
                b.Height = height;//图片高度设置(px单位)
                b.Width = width;//图片宽度设置(px单位)

                image = b.Encode(type, code);//生成图片
                image.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
                image.Dispose();
            }
            catch (Exception ex)
            {

                image = null;
            }
        }

        /// <summary>
        /// 二维码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            //生成二维码 
           // Generate1("这是二维码");

            //生成带logo的二维码 
            Generate3("这是二维码");
          
        }


   
        /// <summary>
        /// 生成二维码,保存成图片
        /// </summary>
        static void Generate1(string text)
        {
            BarcodeWriter writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            QrCodeEncodingOptions options = new QrCodeEncodingOptions();
            options.DisableECI = true;
            //设置内容编码
            options.CharacterSet = "UTF-8";
            //设置二维码的宽度和高度
            options.Width = 500;
            options.Height = 500;
            //设置二维码的边距,单位不是固定像素
            options.Margin = 1;
            writer.Options = options;

            Bitmap map = writer.Write(text);
            string fileSavePath = AppDomain.CurrentDomain.BaseDirectory + "EWM.jpg";
            if (File.Exists(fileSavePath))
                File.Delete(fileSavePath);
            map.Save(fileSavePath, ImageFormat.Png);
            map.Dispose();
        }

        /// <summary>
        /// 读取二维码
        /// 读取失败,返回空字符串
        /// </summary>
        /// <param name="filename">指定二维码图片位置</param>
        static string Read1(string filename)
        {
            BarcodeReader reader = new BarcodeReader();
            reader.Options.CharacterSet = "UTF-8";
            Bitmap map = new Bitmap(filename);
            Result result = reader.Decode(map);
            return result == null ? "" : result.Text;
        }


        //读取内容
        private void button3_Click(object sender, EventArgs e)
        {
            //读取二维码
           // string fileSavePath = AppDomain.CurrentDomain.BaseDirectory + "BarcodePattern11.jpg";

            //读取带logo的二维码
            string fileSavePath = AppDomain.CurrentDomain.BaseDirectory + "logo_Img.jpg";

            //读取条形码码
            //string fileSavePath = AppDomain.CurrentDomain.BaseDirectory + "TXM.jpg";
            string result = Read1(fileSavePath);
            label1.Text = result;
        }


        /// <summary>
        /// 生成带Logo的二维码
        /// </summary>
        /// <param name="text"></param>
        public void Generate3(string text)
        {
           
            //Logo 图片
            Bitmap logo = new Bitmap(AppDomain.CurrentDomain.BaseDirectory + "d06.png");
            //构造二维码写码器
            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);

            //保存成图片
            string fileSavePath = AppDomain.CurrentDomain.BaseDirectory + "logo_Img.jpg";
            if (File.Exists(fileSavePath))
                File.Delete(fileSavePath);
            bmpimg.Save(fileSavePath, ImageFormat.Png);
            pictureBox2.Load(fileSavePath);
            map.Dispose();
        }

 

winform生成条形码和二维码(ZXing.Net)

标签:domain   type   oid   大小   object   代码   odi   system   rate   

原文地址:https://www.cnblogs.com/bin521/p/9875014.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!