C#使用第三方组件生成二维码汇总

用C#如何生成二维码,我们可以通过现有的第三方dll直接来实现,下面列出几种不同的生成方法:

1.通过QrCodeNet(Gma.QrCodeNet.Encoding.dll)来实现

1.1):首先通过VS2015的NuGet下载对应的第三方组件,如下图所示:

1.2):具体生成二维码方法如下

    private void GenerateQRByQrCodeNet()
    {
      QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
      QrCode qrCode = new QrCode();
      qrEncoder.TryEncode("Hello World. This is Eric Sun Testing...", out qrCode);

      GraphicsRenderer renderer = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Two), Brushes.Black, Brushes.White);

      using (MemoryStream ms = new MemoryStream())
      {
        renderer.WriteToStream(qrCode.Matrix, ImageFormat.Png, ms);
        Image img = Image.FromStream(ms);
        img.Save("E:/csharp-qrcode-net.png");
      }
    }

更多详细信息请参考如下链接:

http://qrcodenet.codeplex.com/

http://stackoverflow.com/questions/7020136/free-c-sharp-qr-code-generator

2.通过ThoughtWorks.QRCode(ThoughtWorks.QRCode.dll)来实现

1.1):首先通过VS2015的NuGet下载对应的第三方组件,如下图所示:

1.2):具体生成二维码方法如下

    private void GenerateQRByThoughtWorks()
    {
      QRCodeEncoder encoder = new QRCodeEncoder();
      encoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;//编码方式(注意:BYTE能支持中文,ALPHA_NUMERIC扫描出来的都是数字)
      encoder.QRCodeScale = 4;//大小(值越大生成的二维码图片像素越高)
      encoder.QRCodeVersion = 0;//版本(注意:设置为0主要是防止编码的字符串太长时发生错误)
      encoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;//错误效验、错误更正(有4个等级)
      encoder.QRCodeBackgroundColor = Color.Yellow;
      encoder.QRCodeForegroundColor = Color.Green;
      string qrdata = "Hello 世界! This is Eric Sun Testing....";

      Bitmap bcodeBitmap = encoder.Encode(qrdata.ToString());
      bcodeBitmap.Save(@"E:\HelloWorld.png", ImageFormat.Png);
      bcodeBitmap.Dispose();
    }

3):通过Spire.BarCode(Spire.BarCode.dll)来实现

1.1):首先通过VS2015的NuGet下载对应的第三方组件,如下图所示:

1.2):具体生成二维码方法如下

    private void GenerateQRBySpire()
    {
      BarcodeSettings bs = new BarcodeSettings()
      {
        Data = "This is qr code.",
        Type = BarCodeType.QRCode,
        TopTextColor = Color.Red,
        ShowCheckSumChar = false,
        ShowText = false
      };
      //Generate the barcode based on the this.barCodeControl1
      BarCodeGenerator generator = new BarCodeGenerator(bs);
      Image barcode = generator.GenerateImage();

      //save the barcode as an image
      barcode.Save(@"E:\barcode-2d.png");
    }

1.3):附加具体生成条形码方法如下

    private void GenerateBarCodeBySpire()
    {
      BarcodeSettings bs = new BarcodeSettings()
      {
        Data = "This is barcode.",
        ShowCheckSumChar = false,
        TopTextColor = Color.Red,
        ShowTopText = false,
        ShowTextOnBottom = true
      };
      //Generate the barcode based on the this.barCodeControl1
      BarCodeGenerator generator = new BarCodeGenerator(bs);
      Image barcode = generator.GenerateImage();

      //save the barcode as an image
      barcode.Save(@"E:\barcode.png");
    }

更多详细信息请参考如下链接:

http://freebarcode.codeplex.com/

http://www.e-iceblue.com/Knowledgebase/Spire.BarCode/Program-Guide/Programme-Guide-for-Spire.BarCode.html

3.通过BarcodeLib(BarcodeLib.Barcode.ASP.NET.dll)来实现

下载对应dll的连接为 http://www.barcodelib.com/asp_net/

4.1):具体生成二维码方法如下

    private void GenerateQRByBarcodeLib()
    {
      QRCode qrbarcode = new QRCode();
      qrbarcode.Encoding = QRCodeEncoding.Auto;
      qrbarcode.Data = "336699885522 This is Eric Sun Testing.";

      qrbarcode.ModuleSize = 10;
      qrbarcode.LeftMargin = 8;
      qrbarcode.RightMargin = 8;
      qrbarcode.TopMargin = 8;
      qrbarcode.BottomMargin = 8;
      qrbarcode.ImageFormat = System.Drawing.Imaging.ImageFormat.Gif;

      // Save QR Code barcode image into your system
      qrbarcode.drawBarcode("E:/csharp-qrcode-lib.gif");
    }

4.2):附加具体生成条形码方法如下

    private void GenerateLinearByBarcodeLib()
    {
      Linear barcode = new Linear();
      barcode.Type = BarcodeType.CODE128;
      barcode.Data = "CODE128";
      // other barcode settings.

      // save barcode image into your system
      barcode.drawBarcode("E:/barcode.png");
    }

我们使用的是试用版(带水印的......),还有付费的正版,详情请参考如下链接:

http://www.barcodelib.com/asp_net/