以上图片是本示例中的实际运行效果,在生活中我们的一维码(也就是条形码)、二维码 使用已经非常广泛,那么如何使用c#.net来进行生成一维码(条形码)、二维码呢?
使用ZXing来生成是非常方便的选择,可以在其官网 http://zxingnet.codeplex.com/ 进行下载到,也可以阅读相关的文章,如何解码一维码(条形码)、二维码。一般我会使用VS中的NuGet进行下载。
下载好之后就可以使用了,下面是本示例中的代码:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using ZXing;
- using ZXing.Common;
- using ZXing.QrCode;
- namespace ZXING_Example
- {
- public partial class FrmExample : Form
- {
- public FrmExample()
- {
- InitializeComponent();
- }
- //生成二维码
- private void btnCreateD1_Click(object sender, EventArgs e)
- {
- EncodingOptions options = null;
- BarcodeWriter writer = null;
- options = new QrCodeEncodingOptions
- {
- DisableECI = true,
- CharacterSet = "UTF-8",
- Width = picD2.Width,
- Height = picD2.Height
- };
- writer = new BarcodeWriter();
- writer.Format = BarcodeFormat.QR_CODE;
- writer.Options = options;
- Bitmap bitmap = writer.Write(txtD2.Text);
- picD2.Image = bitmap;
- }
- //生成一维码
- private void btnCreateD2_Click(object sender, EventArgs e)
- {
- EncodingOptions options = null;
- BarcodeWriter writer = null;
- options = new EncodingOptions
- {
- //DisableECI = true,
- //CharacterSet = "UTF-8",
- Width = picD1.Width,
- Height = picD1.Height
- };
- writer = new BarcodeWriter();
- writer.Format = BarcodeFormat.ITF;
- writer.Options = options;
- Bitmap bitmap = writer.Write(txtD1.Text);
- picD1.Image = bitmap;
- }
- }
- }