国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#利用QrCode.Net生成二維碼(Qr碼)的方法

2019-10-29 21:17:50
字體:
來源:轉載
供稿:網友

現在網上很多應用都是用二維碼來分享網址或者其它的信息。尤其在移動領域,二維碼更是有很大的應用場景。因為項目的需要,需要在網站中增加一個生成二維碼分析網址的功能,在谷歌大幅度抽筋的情況下無奈使用百度。百度N多,找到一些項目,但是可用性不強。(有一個項目是用VS2005開發的,在2010中調試不開。)終于在codeplex上找到一個“神器”,這個“神器”可以很方便的生成二維碼,速度那是相當的快,并且可支持中文,遵從MIT協議。

QrCode.Net是一個使用C#編寫的用于生成二維碼圖片的類庫,使用它可以非常方便的為WinForm、WebForm、WPF、Silverlight和Windows Phone 7應用程序提供二維碼編碼輸出功能。可以將二維碼文件導出為eps格式。

項目地址為:http://qrcodenet.codeplex.com

QrCode.Net不再采用http://code.google.com/p/zxing/ ZXing的端口,新的版本將有更好的性能。

測試結果如下(微秒):

輸入字符串長度:74個

EC performance 1000 Tests~ QrCode.Net: 3929 ZXing: 5221

同時,QrCode.Net可以對字符串進行分析,決定是否使用UTF-8編碼。(比如使用中文的時候。)

QrCode使用方法:

新建項目添加對類庫的引用,然后引入Gma.QrCodeNet.Encoding命名空間。

using Gma.QrCodeNet.Encoding;

在控制臺中輸出二維碼:

Console.Write(@"Type some text to QR code: ");string sampleText = Console.ReadLine();QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);QrCode qrCode = qrEncoder.Encode(sampleText);for (int j = 0; j < qrCode.Matrix.Width; j++){for (int i = 0; i < qrCode.Matrix.Width; i++){char charToPrint = qrCode.Matrix[i, j] ? '█' : ' ';Console.Write(charToPrint);}Console.WriteLine();}Console.WriteLine(@"Press any key to quit.");Console.ReadKey();

此代碼將產生以下輸出:

C#,qrcode,生成二維碼

在Graphics上繪制二維碼:

const string helloWorld = "Hello World!";QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);QrCode qrCode = qrEncoder.Encode(helloWorld);const int moduleSizeInPixels = 5;Renderer renderer = new Renderer(moduleSizeInPixels, Brushes.Black, Brushes.White);Panel panel = new Panel();Point padding = new Point(10,16);Size qrCodeSize = renderer.Measure(qrCode.Matrix.Width);panel.AutoSize = false;panel.Size = qrCodeSize + new Size(2 * padding.X, 2 * padding.Y);using (Graphics graphics = panel.CreateGraphics()){renderer.Draw(graphics, qrCode.Matrix, padding);}

在WriteableBitmap上繪制二維碼:

const string helloWorld = "Hello World!";QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);QrCode qrCode = new QrCode();qrEncoder.TryEncode(helloWorld, out qrCode);const int moduleSizeInPixels = 5;Renderer renderer = new Renderer(moduleSizeInPixels); //Black&White is default colour for drawing QrCode//Matrix under qrCode might be null if input string is null or empty. 21 module wide is version 1 QrCode's width. int pixelSize = qrCode.Matrix == null ? renderer.Measure(21) : renderer.Measure(qrCode.Matrix.Width);WriteableBitmap wBitmap = new WriteableBitmap(pixelSize, pixelSize, 96, 96, PixelFormats.Gray8, null);//If wBitmap is null value. renderer will create Gray8 Bitmap as default.renderer.Draw(wBitmap, qrCode.Matrix); //Default offset position is (0, 0);//Now you can put wBitmap to Image control's Source or use it to create image file. 

如果需要把二維碼呈現在WinForm或者WPF應用程序中,可以直接把類庫拖入工具箱,然后直接在窗體上拖出控件。

C#,qrcode,生成二維碼

直接把二維碼保存到文件:

QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);QrCode qrCode = new QrCode();qrEncoder.TryEncode(helloWorld, out qrCode);Renderer renderer = new Renderer(5, Brushes.Black, Brushes.White);renderer.CreateImageFile(qrCode.Matrix, @"c:/temp/HelloWorld.png", ImageFormat.Png);

將二維碼寫入Stream:

QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);QrCode qrCode = new QrCode();qrEncoder.TryEncode(helloWorld, out qrCode);Renderer renderer = new Renderer(5, Brushes.Black, Brushes.White);MemoryStream ms = new MemoryStream();renderer.WriteToStream(qrCode.Matrix, ms, ImageFormat.png);

以上所述是小編給大家介紹的C#使用QrCode.Net生成二維碼(Qr碼),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 静海县| 宁安市| 潼南县| 邵阳县| 吴旗县| 曲松县| 岳阳市| 新巴尔虎左旗| 公安县| 宁夏| 五指山市| 五家渠市| 沈阳市| 乡宁县| 应城市| 崇州市| 松原市| 集贤县| 东乡族自治县| 县级市| 伽师县| 远安县| 佛教| 南漳县| 铅山县| 叙永县| 江北区| 石狮市| 河北区| 泗洪县| 汾阳市| 田东县| 百色市| 邵东县| 泌阳县| 库尔勒市| 东安县| 龙川县| 阆中市| 乌鲁木齐县| 饶河县|