二维码生成器 ZXing.Net 组件应用

c# 二维码生成器(ZXing.Net)实现

安装组件

image

Code

using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using ZXing;
using ZXing.Common;
using ZXing.QrCode.Internal;
namespace WindowsFormsApp1
{
    public partial class Form1 : UIForm
    {
        public Form1()
        {
            InitializeComponent();
        }
        #region 生成二维码方法
        /// <summary>
        /// 生成二维码图片
        /// </summary>
        /// <param name="strMessage">要生成二维码的字符串</param>
        /// <param name="width">二维码图片的宽度(单位:像素)</param>
        /// <param name="height">二维码图片的高度(单位:像素)</param>
        /// <returns></returns>
        private Bitmap GetQRCodeByZXingNet(string strMessage, Int32 width, Int32 height) { 
            Bitmap bitmap = null;
            try {
                BarcodeWriter barcodeWriter = new BarcodeWriter();  //实例化二维码生成对象
                barcodeWriter.Format = BarcodeFormat.QR_CODE;  //生成二维码格式
                barcodeWriter.Options.Hints.Add(EncodeHintType.CHARACTER_SET,"UTF-8"); //指定编码方式
                barcodeWriter.Options.Hints.Add(EncodeHintType.ERROR_CORRECTION,ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
                barcodeWriter.Options.Height = height;
                barcodeWriter.Options.Width = width;
                barcodeWriter.Options.Margin = 0;
                ZXing.Common.BitMatrix bm = barcodeWriter.Encode(strMessage);
                bitmap = barcodeWriter.Write(bm);
                
            }
            catch(Exception ex) {
                MessageBox.Show("生成二维码错误!"+ex.Message);
            }
            return bitmap;
        
        }

        public Bitmap GetQRCodeByZXingNet(string strMessage, Image middleImg, int width, int height) {
            if (string.IsNullOrEmpty(strMessage)){
                return null;
            }
            if(middleImg == null) {
               return GetQRCodeByZXingNet(strMessage, width, height);   

            }
            //实例化二维码写对象
            MultiFormatWriter multiFormatWriter = 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 = multiFormatWriter.encode(strMessage, BarcodeFormat.QR_CODE, width, height, hint);
            BarcodeWriter barcodeWriter = new BarcodeWriter();
            Bitmap bitmap=barcodeWriter.Write(bm);  
            //获取二维码实际尺寸(去掉二维码两边的空白后的尺寸)
            int[] rectangle=bm.getEnclosingRectangle();
            //计算插入图片的大小和位置
            int middleImgW = Math.Min((int)(rectangle[2]/3.5),middleImg.Width);
            int middleImgH = Math.Min((int)(rectangle[3]/3.5),middleImg.Height);
            int middleImgL = (bitmap.Width - middleImgW) / 2;
            int middleImgT = (bitmap.Height - middleImgH) / 2;
            //将Img转换成bmp格式,否则无法创建Graphics 对象
            Bitmap bmpimg=new Bitmap(width, height,System.Drawing.Imaging.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(bitmap, 0, 0);
            }
                //在二维码中插入图片
                Graphics myGraphic = Graphics.FromImage(bmpimg);
                //白底
                myGraphic.FillRectangle(Brushes.White, middleImgL, middleImgT, middleImgW, middleImgH);
                myGraphic.DrawImage(middleImg,middleImgL,middleImgT, middleImgW,middleImgH);
               
           
                return bmpimg;
        }
        #endregion
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void chkPicture_CheckedChanged(object sender, EventArgs e)
        {
            if (chkPicture.Checked)
            {
                uiGroupBox2.Visible = true;
            }
            else
            {
                uiGroupBox2.Visible = false;
            }
        }

        private void btnGeneralBarCode_Click(object sender, EventArgs e)
        {
            Bitmap qrCode;
            if (chkPicture.Checked)
            {
                qrCode = GetQRCodeByZXingNet(txtContents.Text, pictureBox2.Image, Convert.ToInt32(txtWidth.Text), Convert.ToInt32(txtHeight.Text));

            }
            else
            {
                qrCode = GetQRCodeByZXingNet(txtContents.Text, Convert.ToInt32(txtWidth.Text), Convert.ToInt32(txtHeight.Text));

            }
            qrCode.Save("qr.png",ImageFormat.Png);
            pictureBox1.Image = qrCode;
        }

        private void btnSelectLogo_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = "图片文件|*.jpg;*.jpeg;*.png;*.bmp";
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                pictureBox2.Image = Image.FromFile(openFile.FileName);
            }
        }
    }
}

运行效果

image

posted @ 2024-06-01 18:00  丹心石  阅读(11)  评论(0编辑  收藏  举报