新建一個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