C# winform点击生成二维码

新建一个winform程序:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            txtQRInfo.Text = "SB2021020221500001";
            btn_MakeQRCode_Click(null, null);
        }

        //生成二维码
        private void btn_MakeQRCode_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtQRInfo.Text)) {
                MessageBox.Show("请输入要生成的二维码!");
                return;
            }
            Bitmap img = QRCode.GenByZXingNet(txtQRInfo.Text);
            pictureBox1.Image = img;
        }
    }



   //二维码生成封装类
    public class QRCode
    {

        public static Bitmap GenByZXingNet(string msg)
        {
            BarcodeWriter writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            writer.Options.Hints.Add(EncodeHintType.CHARACTER_SET,"UTF-8");//编码问题
            writer.Options.Hints.Add(EncodeHintType.ERROR_CORRECTION,ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
            const int codeSizeInPixels = 120;//设置图片长度
            writer.Options.Height = writer.Options.Width = codeSizeInPixels;
            writer.Options.Margin = 1;//设置边框
            ZXing.Common.BitMatrix bm = writer.Encode(msg);
            Bitmap img = writer.Write(bm);
            return img;
        }
    }

附:生成二维码并打印:https://www.cnblogs.com/xixim/p/4589078.html

posted @ 2021-02-02 21:56  朕在coding  阅读(989)  评论(0编辑  收藏  举报