C#通过第三方组件生成二维码(QR Code)和条形码(Bar Code)
用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://www.cnblogs.com/Soar1991/archive/2012/03/30/2426115.html
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: H2AMK-Z3V69-RTJZD-C7JAU-WILL4", 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: H2AMK-Z3V69-RTJZD-C7JAU-WILL4", 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"); }
1.3):上诉代码我们发现生成的条形码和二维码带有水印[E-ICEBLUE],如何去除水印呢?请看如下代码
BarcodeSettings.ApplyKey("......");
请发送邮件到 sales@e-iceblue.com 免费获取对应的 key 值
更多详细信息请参考如下链接:
http://freebarcode.codeplex.com/
http://www.e-iceblue.com/Knowledgebase/Spire.BarCode/Program-Guide/Programme-Guide-for-Spire.BarCode.html
http://blog.csdn.net/eiceblue/article/details/43405173
4):通过Barcode Rendering Framework(Zen.Barcode.Rendering.Framework.dll)来实现
4.1):首先通过VS2015的NuGet下载对应的第三方组件,如下图所示:
4.2):具体生成二维码方法如下
private void GenerateBarCodeByZen() { Code128BarcodeDraw barcode128 = BarcodeDrawFactory.Code128WithChecksum; Image img = barcode128.Draw("Hello World", 40); img.Save("E:/zenbarcode.gif"); }
4.3):附加具体生成条形码方法如下
private void GenerateQRByZen() { CodeQrBarcodeDraw qrcode = BarcodeDrawFactory.CodeQr; Image img = qrcode.Draw("Hello World!", qrcode.GetDefaultMetrics(40)); img.Save("E:/zenqrcode.gif"); }
更多详细信息请参考如下链接:
http://barcoderender.codeplex.com/
5):通过BarcodeLib(BarcodeLib.Barcode.ASP.NET.dll)来实现,下载对应dll的连接为 http://www.barcodelib.com/asp_net/
5.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"); }
5.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/