用C#如何生成二維碼,咱們能夠經過現有的第三方dll直接來實現,下面列出幾種不一樣的生成方法:html
1):經過QrCodeNet(Gma.QrCodeNet.Encoding.dll)來實現ide
1.1):首先經過VS2015的NuGet下載對應的第三方組件,以下圖所示:ui
1.2):具體生成二維碼方法以下this
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.htmlspa
http://qrcodenet.codeplex.com/ .net
http://stackoverflow.com/questions/7020136/free-c-sharp-qr-code-generatorcode
2):經過ThoughtWorks.QRCode(ThoughtWorks.QRCode.dll)來實現orm
1.1):首先經過VS2015的NuGet下載對應的第三方組件,以下圖所示:htm
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/