C#生成二维码

该项目大部分来源于网络。
参考了:https://blog.csdn.net/jun502525164/article/details/22897225

一、新建项目:二维码生成
二、下载ZXing,并引用
下载地址:https://pan.baidu.com/s/1rqlmhuiv4UndJ8deimYFgA 提取码:jgim
三、窗体如下图布局:
在这里插入图片描述
注:TextBox 和ComboBox名称按照从左到右,从上到下的顺序分别为S_1到S_9。
四、分别新建MyModule和BarCodeClass类
代码如下:
(1)MyModule:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Forms;

using ZXing.Common;
using ZXing;
using ZXing.QrCode;

using System.Drawing;

namespace 二维码生成
{
    class MyModule
    {
        #region 生成二维码信息
        /// <summary>
        /// 生成二维码信息
        /// </summary>
        /// <param name="Contr">指定控件的数据集</param>
        /// <param name="TName">控件不分名称</param>
        /// <param name="n">控件的个数</param>
        public static string GetInfo(int n, Control.ControlCollection GBox, string TName)
        {
            //ZgInfo = S_1.Text + "\r\n" + S_2.Text + "\r\n" + S_3.Text + "\r\n" + S_4.Text + "\r\n" + S_5.Text + "\r\n" + S_6.Text + "\r\n" + S_7.Text + "\r\n" + S_8.Text + "\r\n" + S_9.Text;
            string  ZgInfo = "";
            for (int i = 1; i <= n; i++)
            {
                string str = TName + i.ToString();
                foreach (Control c in GBox)
                {
                    if (c.Name == str)
                    {
                        ZgInfo += (c.Text + "\r\n");
                    }
                }
            }
            return ZgInfo;
        }
 #endregion
 #region 清空控件
/// <summary>
/// 清空控件
/// </summary>
/// <param name="GBox">指定控件的数据集</param>
/// <param name="TName">控件不分名称</param>
/// <param name="n">控件的个数</param>
public static void Clear_Grids(int n, Control.ControlCollection GBox, string TName)
    {
        string sID = "";
        for (int i = 1; i <= n; i++)
        {
            sID = TName + i.ToString();
            foreach (Control C in GBox)
            {
                if (C.GetType().Name == "TextBox" | C.GetType().Name == "ComboBox")
                    if (C.Name == sID)
                    {
                        C.Text = "";
                    }
            }
        }
    }
    #endregion

    #region 生成二维码
    /// <summary>
    /// 生成二维码信息
    /// </summary>
    /// <param name="GBox">指定控件的数据集</param>
    /// <param name="TName">控件不分名称</param>
    /// <param name="n">控件的个数</param>
    /// <param name="PBox1">图片框名称</param>
    public static void AddInfo(PictureBox PBox1, int n, Control.ControlCollection GBox, string TName)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Filter = "图像文件(*.png;*.jpg;)|*.png;*.jpg";
        //文本文件(*.txt)|*.txt|所有文件(*.*)|*.*”
        ofd.ShowDialog();
        if (ofd.FileName != null && ofd.FileName != "")
        {
            string path = ofd.FileName;

            PBox1.Load(path);

            if (PBox1.Image == null)
            {
                MessageBox.Show("请录入图像后再进行解码!");
                return;
            }
            BarcodeReader reader = new BarcodeReader();
            Result result = reader.Decode((Bitmap)PBox1.Image);
            string[] info = result.Text.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            string CName = "";
            for (int i = 1; i <= n; i++)
            {
                CName = TName + i.ToString();
                foreach (Control C in GBox)
                {
                    if (C.GetType().Name == "TextBox" | C.GetType().Name == "ComboBox")
                        if (C.Name == CName)
                        {
                            C.Text = info[i - 1];
                        }
                }
            }
        }
    }
    #endregion
    }
}

(2)BarCodeClass.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using ZXing.Common;
using ZXing;
using ZXing.QrCode;

namespace 二维码生成
{
    class BarCodeClass
    {
#region 二维码宽度
 /// <summary>
///  二维码宽度
/// </summary>
public int QuickMarkWidth { get; set; }
#endregion

    #region 二维码高度
    /// <summary>
    /// 二维码高度
    /// </summary>
    public int QuickMarkHeight { get; set; }
    #endregion

    #region 生成条形码
    ///<summary>
    ///生成条形码
    ///</summary>
    ///<paramname="pictureBox1"></param>
    ///<paramname="Contents"></param>
    public void CreateBarCode(PictureBox pictureBox1, string Contents)
    {
        //Regex rg = new Regex("^[0-9]{20}$");
        //if (!rg.IsMatch(Contents))
        //{
        //    MessageBox.Show("本例子采用QR_CODE编码,需要输入12位数字");
        //    return;
        //}

        EncodingOptions options = null;
        BarcodeWriter writer = null;
        if (BarCodeWidth == 0 || BarCodeHeight == 0)
        {
            BarCodeWidth = pictureBox1.Width;
            BarCodeHeight = pictureBox1.Height;
        }
        options = new EncodingOptions
        {
            Width = BarCodeWidth,
            Height = BarCodeHeight
        };
        writer = new BarcodeWriter();
        writer.Format = BarcodeFormat.CODE_128;
        writer.Options = options;

        Bitmap bitmap = writer.Write(Contents);
        pictureBox1.Image = bitmap;
    }
    #endregion

    #region 生成二维码
    ///<summary>
    ///生成二维码
    ///</summary>
    ///<paramname="pictureBox1"></param>
    ///<paramname="Contents"></param>
    public void CreateQuickMark(PictureBox pictureBox1, string Contents)
    {
        if (Contents == string.Empty)
        {
            MessageBox.Show("输入内容不能为空!");
            return;
        }

        EncodingOptions options = null;
        BarcodeWriter writer = null;
        if (BarCodeWidth == 0 || BarCodeHeight == 0)
        {
            QuickMarkWidth = pictureBox1.Height;
            QuickMarkHeight = pictureBox1.Height;
        }
        options = new QrCodeEncodingOptions
        {
            DisableECI = true,
            CharacterSet = "UTF-8",
            Width = QuickMarkWidth,
            Height = QuickMarkHeight,
            //Left
        };
        writer = new BarcodeWriter();
        writer.Format = BarcodeFormat.QR_CODE;
        writer.Options = options;


        Bitmap bitmap = writer.Write(Contents);
        pictureBox1.Image = bitmap;
    }
    #endregion
    
    #region 解码
    ///<summary>
    ///解码
    ///</summary>
    ///<paramname="pictureBox1"></param>
    public void Decode(PictureBox pictureBox1)
    {
        BarcodeReader reader = new BarcodeReader();
        Result result = reader.Decode((Bitmap)pictureBox1.Image);
    }
    #endregion
}
}

五、在窗体相应按钮下调用方法即可。
有疑问欢迎留言。祝您成功!
源码下载地址:https://download.csdn.net/download/yd1300/10708384

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值