Currently, many online applications use QR codes to share websites or other information. In the mobile field, the QR code has many application scenarios. Because of the needs of the project, you need to add a function to the website to generate a QR code to analyze the Web site. In the case of Google's Severe cramps, you are helpless to use Baidu. Baidu n many, found some projects, but the availability is not strong. (One Project was developed with vs2005 and cannot be debugged in 2010 .) Finally, I found an "artifact" on codeplex. This "artifact" can easily generate two-dimensional codes. The speed is quite fast. It supports Chinese characters and complies with the MIT protocol.
Qrcode. net is a class library written in C # To generate QR code images, it provides the QR code output function for winform, webform, WPF, Silverlight, and Windows Phone 7 applications. You can export the QR code file to the EPS format.
Project address: http://qrcodenet.codeplex.com
Qrcode. Net no longer uses the http://code.google.com/p/zxing/ zxing port, the new version will have better performance.
The test results are as follows (microseconds ):
Input String Length: 74
EC performance 1000 tests ~ Qrcode. Net: 3929 ZX ing: 5221
At the same time, qrcode. Net can analyze strings and decide whether to use UTF-8 encoding. (For example, when using Chinese .)
Qrcode usage:
Create a project, add a reference to the class library, and then introduceGMA. qrcodenet. EncodingNamespace.
using Gma.QrCodeNet.Encoding;
Output the QR code on the console:
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();
This code generates the following output:
Draw a QR code on 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); }
Draw a QR code on 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.
If you need to present the QR code in a winform or WPF application, you can drag the class library into the toolbox and drag the control directly on the form.
Directly Save the QR code to the file:
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);
Write the QR code to 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);