C#使用第三方組件生成二維碼匯總
用C#如何生成二維碼,我們可以通過(guò)現(xiàn)有的第三方dll直接來(lái)實(shí)現(xiàn),下面列出幾種不同的生成方法:
1.通過(guò)QrCodeNet(Gma.QrCodeNet.Encoding.dll)來(lái)實(shí)現(xiàn)
1.1):首先通過(guò)VS2015的NuGet下載對(duì)應(yīng)的第三方組件,如下圖所示:

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");
}
}
更多詳細(xì)信息請(qǐng)參考如下鏈接:
http://qrcodenet.codeplex.com/
http://stackoverflow.com/questions/7020136/free-c-sharp-qr-code-generator
2.通過(guò)ThoughtWorks.QRCode(ThoughtWorks.QRCode.dll)來(lái)實(shí)現(xiàn)
1.1):首先通過(guò)VS2015的NuGet下載對(duì)應(yīng)的第三方組件,如下圖所示:

1.2):具體生成二維碼方法如下
private void GenerateQRByThoughtWorks()
{
QRCodeEncoder encoder = new QRCodeEncoder();
encoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;//編碼方式(注意:BYTE能支持中文,ALPHA_NUMERIC掃描出來(lái)的都是數(shù)字)
encoder.QRCodeScale = 4;//大小(值越大生成的二維碼圖片像素越高)
encoder.QRCodeVersion = 0;//版本(注意:設(shè)置為0主要是防止編碼的字符串太長(zhǎng)時(shí)發(fā)生錯(cuò)誤)
encoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;//錯(cuò)誤效驗(yàn)、錯(cuò)誤更正(有4個(gè)等級(jí))
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):通過(guò)Spire.BarCode(Spire.BarCode.dll)來(lái)實(shí)現(xiàn)
1.1):首先通過(guò)VS2015的NuGet下載對(duì)應(yīng)的第三方組件,如下圖所示:

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");
}
更多詳細(xì)信息請(qǐng)參考如下鏈接:
http://freebarcode.codeplex.com/
http://www.e-iceblue.com/Knowledgebase/Spire.BarCode/Program-Guide/Programme-Guide-for-Spire.BarCode.html
3.通過(guò)BarcodeLib(BarcodeLib.Barcode.ASP.NET.dll)來(lái)實(shí)現(xiàn)
下載對(duì)應(yīng)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");
}
我們使用的是試用版(帶水印的......),還有付費(fèi)的正版,詳情請(qǐng)參考如下鏈接:
http://www.barcodelib.com/asp_net/
相關(guān)文章
C# 實(shí)現(xiàn)簡(jiǎn)易的串口監(jiān)視上位機(jī)功能附源碼下載
這篇文章主要介紹了C# 實(shí)現(xiàn)簡(jiǎn)易的串口監(jiān)視上位機(jī)功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
詳解C#如何實(shí)現(xiàn)隱式類(lèi)型轉(zhuǎn)換
Result?類(lèi)型是許多編程語(yǔ)言中處理錯(cuò)誤的常用方式,包括?C#?的?dotNext?庫(kù)。在本文中,我們將通過(guò)例子回顧?C#?中?using?語(yǔ)句和隱式類(lèi)型轉(zhuǎn)換的使用,感興趣的可以了解一下2023-01-01
Unity實(shí)現(xiàn)移動(dòng)物體到鼠標(biāo)點(diǎn)擊位置
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)移動(dòng)物體到鼠標(biāo)點(diǎn)擊位置,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08
使用checked語(yǔ)句防止數(shù)據(jù)溢出的解決方法
本篇文章是對(duì)用checked語(yǔ)句防止數(shù)據(jù)溢出的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C#如何Task執(zhí)行任務(wù),等待任務(wù)完成
這篇文章主要介紹了C#如何Task執(zhí)行任務(wù),等待任務(wù)完成,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06

