C# 使用ZXing.NET生成一维码、二维码

ZXing.net以上图片是本示例中的实际运行效果,在生活中我们的一维码(也就是条形码)、二维码 使用已经非常广泛,那么如何使用c#.net来进行生成一维码(条形码)、二维码呢?

使用ZXing来生成是非常方便的选择,可以在其官网 http://zxingnet.codeplex.com/ 进行下载到,也可以阅读相关的文章,如何解码一维码(条形码)、二维码。一般我会使用VS中的NuGet进行下载

 

下载好之后就可以使用了,下面是本示例中的代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using ZXing;
  11. using ZXing.Common;
  12. using ZXing.QrCode;
  13.  
  14.  
  15. namespace ZXING_Example
  16. {
  17.     public partial class FrmExample : Form
  18.     {
  19.         public FrmExample()
  20.         {
  21.             InitializeComponent();
  22.         }
  23.  
  24.         //生成二维码
  25.         private void btnCreateD1_Click(object sender, EventArgs e)
  26.         {
  27.             EncodingOptions options = null;
  28.             BarcodeWriter writer = null;
  29.  
  30.             options = new QrCodeEncodingOptions
  31.             {
  32.                 DisableECI = true,
  33.                 CharacterSet = "UTF-8",
  34.                 Width = picD2.Width,
  35.                 Height = picD2.Height
  36.             };
  37.             writer = new BarcodeWriter();
  38.             writer.Format = BarcodeFormat.QR_CODE;
  39.             writer.Options = options;
  40.  
  41.             Bitmap bitmap = writer.Write(txtD2.Text);
  42.             picD2.Image = bitmap;
  43.  
  44.         }
  45.  
  46.         //生成一维码
  47.         private void btnCreateD2_Click(object sender, EventArgs e)
  48.         {
  49.             EncodingOptions options = null;
  50.             BarcodeWriter writer = null;
  51.  
  52.             options = new EncodingOptions
  53.             {
  54.                 //DisableECI = true,  
  55.                 //CharacterSet = "UTF-8",  
  56.                 Width = picD1.Width,
  57.                 Height = picD1.Height
  58.             };
  59.             writer = new BarcodeWriter();
  60.             writer.Format = BarcodeFormat.ITF;
  61.             writer.Options = options;
  62.  
  63.             Bitmap bitmap = writer.Write(txtD1.Text);
  64.             picD1.Image = bitmap;
  65.         }
  66.     }
  67. }

posted on 2014-11-03 13:54  ExplorerMan  阅读(1792)  评论(0编辑  收藏  举报

导航