二维码现在用途很多,怎么用C#编程生成呢?
准备 ThoughtWorks.QRCode.dll
需要 一个类 QRCode,这个类专门来生成二维码的
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThoughtWorks.QRCode;
using ThoughtWorks.QRCode.Codec; namespace QRCodePro
{
public class QRCode
{
/// 生成二维码
/// </summary>
/// <param name="strData">要生成的文字或者数字</param>
/// <param name="qrEncoding">三种尺寸:BYTE ,ALPHA_NUMERIC,NUMERIC</param>
/// <param name="level">大小:L M Q H</param>
/// <param name="version">版本:如 8</param>
/// <param name="scale">比例:如 4</param>
/// <returns></returns>
public static Image CreateCode_Choose(string strData, string qrEncoding, string size, int version, int scale)
{
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
string encoding = qrEncoding;
switch (encoding)
{
case "Byte":
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
break;
case "AlphaNumeric":
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.ALPHA_NUMERIC;
break;
case "Numeric":
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.NUMERIC;
break;
default:
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
break;
} qrCodeEncoder.QRCodeScale = scale;
qrCodeEncoder.QRCodeVersion = version;
switch (size)
{
case "L":
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;
break;
case "M":
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
break;
case "Q":
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.Q;
break;
default:
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;
break;
}
//文字生成图片
Image image = qrCodeEncoder.Encode(strData);
return image;
}
}
}
在vs中新建窗体,搭建窗体,如图所示:
在主窗体中需要的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace QRCodePro
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
cboEncoding.Items.Add("Byte");
cboEncoding.Items.Add("AlphaNumeric");
cboEncoding.Items.Add("Numeric");
cboEncoding.SelectedIndex = ;
//填充大小下拉框
cboSize.Items.Add("M");
cboSize.Items.Add("L");
cboSize.Items.Add("Q");
cboSize.Items.Add("H");
cboSize.SelectedIndex = ;
//填充文本框
txtVersion.Text = "";
txtScale.Text = "";
} private void btnCreateCode_Click(object sender, EventArgs e)
{
string content = txtContent.Text;
int version = Convert.ToInt32(txtVersion.Text);
int scale = Convert.ToInt32(txtScale.Text);
string size = cboSize.SelectedItem.ToString();
string encoding = cboEncoding.SelectedItem.ToString();
//检查空值字符串
if (content == null || content == "")
{
MessageBox.Show("请输入二维码信息,支持中文");
}
else
{
Image img = QRCode.CreateCode_Choose(content, encoding, size, version, scale);
picCode.Image = img;
}
} private void btnReset_Click(object sender, EventArgs e)
{
txtContent.Text = "";
} private void btnSave_Click(object sender, EventArgs e)
{
if (picCode.Image != null)
{
SaveImages(picCode);
}
else
{
MessageBox.Show("还没生成图像呢!");
}
}
private void SaveImages(PictureBox pic)
{
string Parentpath = @"D:\Photo";
//自1970 年1月1日00:00:00 GMT以来的毫秒数
string filename = DateTime.Now.Ticks.ToString();
if (Parentpath.Substring(Parentpath.Length - , ) != @"\")
Parentpath = Parentpath + @"\";
string childPath = Parentpath + DateTime.Now.ToString("yyyyMMdd");
if (!Directory.Exists(childPath))
Directory.CreateDirectory(childPath);
pic.Image.Save(childPath + "\\" + filename + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
MessageBox.Show("保存成功!");
}
}
}
实现的效果
保持文件
要把此项目移植到asp.net网站上也是极其简单的,这里就不再演示了。